Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
56
Task/Truncatable-primes/FreeBASIC/truncatable-primes-1.basic
Normal file
56
Task/Truncatable-primes/FreeBASIC/truncatable-primes-1.basic
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function isPrime(n As Integer) As Boolean
|
||||
If n Mod 2 = 0 Then Return n = 2
|
||||
If n Mod 3 = 0 Then Return n = 3
|
||||
Dim d As Integer = 5
|
||||
While d * d <= n
|
||||
If n Mod d = 0 Then Return False
|
||||
d += 2
|
||||
If n Mod d = 0 Then Return False
|
||||
d += 4
|
||||
Wend
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Dim As UInteger i, j, p, pow, lMax = 2, rMax = 2
|
||||
Dim s As String
|
||||
|
||||
' largest left truncatable prime less than 1000000
|
||||
' It can't end with 1, 4, 6, 8 or 9 as these numbers are not prime
|
||||
' Nor can it end in 2 if it has more than one digit as such a number would divide by 2
|
||||
For i = 3 To 999997 Step 2
|
||||
s = Str(i)
|
||||
If Instr(s, "0") > 1 Then Continue For '' cannot contain 0
|
||||
j = s[Len(s) - 1] - 48
|
||||
If j = 1 OrElse j = 9 Then Continue For
|
||||
p = i
|
||||
pow = 10 ^ (Len(s) - 1)
|
||||
While pow > 1
|
||||
If Not isPrime(p) Then Continue For
|
||||
p Mod= pow
|
||||
pow \= 10
|
||||
Wend
|
||||
lMax = i
|
||||
Next
|
||||
|
||||
' largest right truncatable prime less than 1000000
|
||||
' It can't begin with 1, 4, 6, 8 or 9 as these numbers are not prime
|
||||
For i = 3 To 799999 Step 2
|
||||
s = Str(i)
|
||||
If Instr(s, "0") > 1 Then Continue For '' cannot contain 0
|
||||
j = s[0] - 48
|
||||
If j = 1 OrElse j = 4 OrElse j = 6 Then Continue For
|
||||
p = i
|
||||
While p > 0
|
||||
If Not isPrime(p) Then Continue For
|
||||
p \= 10
|
||||
Wend
|
||||
rMax = i
|
||||
Next
|
||||
|
||||
Print "Largest left truncatable prime : "; lMax
|
||||
Print "Largest right truncatable prime : "; rMax
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
83
Task/Truncatable-primes/FreeBASIC/truncatable-primes-2.basic
Normal file
83
Task/Truncatable-primes/FreeBASIC/truncatable-primes-2.basic
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
' version 10-12-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
Dim Shared As Byte isPrime()
|
||||
|
||||
Sub sieve(m As UInteger)
|
||||
|
||||
Dim As Integer i, j
|
||||
ReDim isPrime(m)
|
||||
|
||||
For i = 4 To m Step 2
|
||||
isPrime(i) = 1
|
||||
Next
|
||||
|
||||
For i = 3 To Sqr(m) Step 2
|
||||
If isPrime(i) = 0 Then
|
||||
For j = i * i To m Step i * 2
|
||||
isPrime(j) = 1
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
#Define max 1000000 'upto 2^30 max for 32bit OS
|
||||
|
||||
Dim As UInteger a(), lt_prime(5000), rt_prime(100)
|
||||
Dim As UInteger i, j, j1, p1, p2, left_max, right_max
|
||||
|
||||
sieve(max)
|
||||
|
||||
' left truncatable primes
|
||||
' if odd and ends with 3 or 7, never ends 1 or 9 (no prime
|
||||
' never ends on a 2 or 5 and starts with 1 to 9
|
||||
lt_prime(1) = 3 : lt_prime(2) = 7
|
||||
p1 = 1 : p2 = 2
|
||||
|
||||
Do
|
||||
For i = 1 To 9
|
||||
j = Val( Str(i) + Str(lt_prime(p1)) )
|
||||
If j > max Then Exit Do
|
||||
If isPrime(j) = 0 Then ' if prime then add to the list
|
||||
p2 += 1
|
||||
lt_prime(p2) = j
|
||||
If Left_max < j Then left_max = j
|
||||
End If
|
||||
Next
|
||||
p1 += 1
|
||||
Loop Until p1 > p2 ' no more numbers to process
|
||||
|
||||
' right truncatable prime
|
||||
' start with 2, 3, 5 or 7 and end with 1, 3, 7 or 9
|
||||
rt_prime(1) = 2 : rt_prime(2) = 3 : rt_prime(3) = 5 : rt_prime(4) = 7
|
||||
p1 = 1 : p2 = 4
|
||||
Dim As UInteger end_num(1 To 4) => {1, 3, 7, 9}
|
||||
|
||||
Do
|
||||
j1 = rt_prime(p1) * 10
|
||||
If j1 > max Then Exit Do
|
||||
For i = 1 To 4
|
||||
j = j1 + End_num(i)
|
||||
If isprime(j) = 0 Then ' if prime then add to the list
|
||||
p2 += 1
|
||||
rt_prime(p2) = j
|
||||
' If right_max < j Then right_max = j
|
||||
End If
|
||||
Next
|
||||
p1 += 1
|
||||
Loop Until p1 > p2 ' no more numbers to process
|
||||
' the last one added is the biggest
|
||||
right_max = rt_prime(p2)
|
||||
|
||||
Print
|
||||
Print "The biggest left truncatable prime below"; max; " is "; left_max
|
||||
Print "The biggest right truncatable prime below"; max; " is "; right_max
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
Loading…
Add table
Add a link
Reference in a new issue