Strong and weak primes: Difference between revisions

add FreeBASIC
(added AWK)
(add FreeBASIC)
Line 699:
37,780 weak primes below 1,000,000
321,750 weak primes below 10,000,000
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>
#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
'finds the next prime after n, excluding n if it happens to be prime itself
if n = 0 then return 2
if n < 3 then return n + 1
dim as integer q = n + 2
while not isprime(q)
q+=2
wend
return q
end function
 
function lastprime( n as uinteger ) as uinteger
'finds the last prime before n, excluding n if it happens to be prime itself
if n = 2 then return 0 'zero isn't prime, but it is a good sentinel value :)
if n = 3 then return 2
dim as integer q = n - 2
while not isprime(q)
q-=2
wend
return q
end function
 
function isstrong( p as integer ) as boolean
if nextprime(p) + lastprime(p) >= 2*p then return false else return true
end function
 
function isweak( p as integer ) as boolean
if nextprime(p) + lastprime(p) <= 2*p then return false else return true
end function
 
print "The first 36 strong primes are: "
dim as uinteger c, p=3
while p < 10000000
if isprime(p) andalso isstrong(p) then
c += 1
if c <= 36 then print p;" ";
if c=37 then print
end if
if p = 1000001 then print "There are ";c;" strong primes below one million"
p+=2
wend
print "There are ";c;" strong primes below ten million"
print
print "The first 37 weak primes are: "
p=3 : c=0
while p < 10000000
if isprime(p) andalso isweak(p) then
c += 1
if c <= 37 then print p;" ";
if c=38 then print
end if
 
if p = 1000001 then print "There are ";c;" weak primes below one million"
p+=2
wend
print "There are ";c;" weak primes below ten million"
print</lang>
{{out}}<pre>
The first 36 strong primes are:
11 17 29 37 41 59 67 71 79 97 101 107 127 137 149 163 179 191 197 223 227 239 251 269 277 281 307 311 331 347 367 379 397 419 431 439
There are 37723 strong primes below one million
There are 320991 strong primes below ten million
 
The first 37 weak primes are:
3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131 139 151 167 181 193 199 229 233 241 271 283 293 313 317 337 349 353 359 383 389 401
There are 37780 weak primes below one million
There are 321750 weak primes below ten million
</pre>
 
781

edits