Safe primes and unsafe primes: Difference between revisions

Added Easylang
(added Arturo)
(Added Easylang)
 
(4 intermediate revisions by 3 users not shown)
Line 925:
There are 74174 unsafe primes below 1000000
There are 633922 unsafe primes below 10000000</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{Uses seive object that creates an array of flags that tells whether a particular number is prime or not}
 
 
function GetSafeUnsafePrimes(MaxCount,MaxPrime: integer; ShowSafe, Display: boolean): string;
{MaxCount = Maximum number of Safe/Unsafe primes to find}
{MaxPrime = Maximum number of primes tested}
{ShowSafe = Controls whether we are looking for Save/Unsafe primes}
{Display = Controls whether the primes are displayed or just counted}
var I,Cnt: integer;
var Sieve: TPrimeSieve;
var IsSafe: boolean;
 
procedure CountAndDisplay;
begin
Inc(Cnt);
if Display then Result:=Result+Format('%7D',[I]);
If Display and ((Cnt mod 5)=0) then Result:=Result+CRLF;
end;
 
begin
{Create sieve and set sieve size}
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(MaxPrime*2);
Cnt:=0;
Result:='';
I:=2;
while true do
begin
if I>=MaxPrime then break;
IsSafe:=(I>3) and Sieve[(I-1) div 2];
if IsSafe=ShowSafe then CountAndDisplay;
if Cnt>=MaxCount then break;
I:=Sieve.NextPrime(I);
end;
Result:=Result+'Count = '+FloatToStrF(Cnt,ffNumber,18,0);
finally Sieve.Free; end;
end;
 
 
procedure ShowSafeUnsafePrimes(Memo: TMemo);
var S: string;
begin
Memo.Lines.Add('The first 35 safe primes: ');
S:=GetSafeUnsafePrimes(35,10000000,True,True);
Memo.Lines.Add(S);
Memo.Lines.Add('Safe Primes Under 1,000,000: ');
S:=GetSafeUnsafePrimes(high(integer),1000000,True,False);
Memo.Lines.Add(S);
Memo.Lines.Add('Safe Primes Under 10,000,000: ');
S:=GetSafeUnsafePrimes(high(integer),10000000,True,False);
Memo.Lines.Add(S);
 
Memo.Lines.Add('The first 40 Unsafe primes: ');
S:=GetSafeUnsafePrimes(40,10000000,False,True);
Memo.Lines.Add(S);
Memo.Lines.Add('Unsafe Primes Under 1,000,000: ');
S:=GetSafeUnsafePrimes(high(integer),1000000,False,False);
Memo.Lines.Add(S);
Memo.Lines.Add('Unsafe Primes Under 10,000,000: ');
S:=GetSafeUnsafePrimes(high(integer),10000000,False,False);
Memo.Lines.Add(S);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
The first 35 safe primes:
5 7 11 23 47
59 83 107 167 179
227 263 347 359 383
467 479 503 563 587
719 839 863 887 983
1019 1187 1283 1307 1319
1367 1439 1487 1523 1619
Count = 35
Safe Primes Under 1,000,000:
Count = 4,324
Safe Primes Under 10,000,000:
Count = 30,657
The first 40 Unsafe primes:
2 3 13 17 19
29 31 37 41 43
53 61 67 71 73
79 89 97 101 103
109 113 127 131 137
139 149 151 157 163
173 181 191 193 197
199 211 223 229 233
Count = 40
Unsafe Primes Under 1,000,000:
Count = 74,174
Unsafe Primes Under 10,000,000:
Count = 633,922
Elapsed Time: 816.075 ms.
</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
if num mod 2 = 0
if num = 2
return 1
.
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
print "First 35 safe primes:"
for i = 2 to 999999
if isprim i = 1 and isprim ((i - 1) / 2) = 1
if count < 35
write i & " "
.
count += 1
.
.
print ""
print "There are " & count & " safe primes below 1000000"
for i = i to 9999999
if isprim i = 1 and isprim ((i - 1) / 2) = 1
count += 1
.
.
print "There are " & count & " safe primes below 10000000"
print ""
count = 0
print "First 40 unsafe primes:"
for i = 2 to 999999
if isprim i = 1 and isprim ((i - 1) / 2) = 0
if count < 40
write i & " "
.
count += 1
.
.
print ""
print "There are " & count & " unsafe primes below 1000000"
for i = i to 9999999
if isprim i = 1 and isprim ((i - 1) / 2) = 0
count += 1
.
.
print "There are " & count & " unsafe primes below 10000000"
</syntaxhighlight>
 
{{out}}
<pre>
First 35 safe primes:
5 7 11 23 47 59 83 107 167 179 227 263 347 359 383 467 479 503 563 587 719 839 863 887 983 1019 1187 1283 1307 1319 1367 1439 1487 1523 1619
There are 4324 safe primes below 1000000
There are 30657 safe primes below 10000000
 
First 40 unsafe primes:
2 3 13 17 19 29 31 37 41 43 53 61 67 71 73 79 89 97 101 103 109 113 127 131 137 139 149 151 157 163 173 181 191 193 197 199 211 223 229 233
There are 74174 unsafe primes below 1000000
There are 633922 unsafe primes below 10000000
</pre>
 
=={{header|F_Sharp|F#}}==
Line 2,386 ⟶ 2,563:
</pre>
 
 
 
{{works with|HP|49g}}
1 - 2 /
'''IFERR''' ISPRIME? '''THEN''' DROP 0 '''END'''
≫ '<span style="color:blue">SAFE?</span>' STO
≪ → function count
≪ { } 2
'''WHILE''' OVER SIZE count < '''REPEAT '''
'''IF''' DUP function EVAL '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''END'''
DROP
≫ ≫ '<span style="color:blue">FIRSTSEQ</span>' STO
≪ → function max
≪ 0 2
'''WHILE''' DUP max < '''REPEAT '''
'''IF''' DUP function EVAL '''THEN''' SWAP 1 + SWAP '''END'''
NEXTPRIME
'''END'''
DROP
≫ ≫ '<span style="color:blue">CNTSEQ</span>' STO
 
≪ <span style="color:blue">SAFE?</span> ≫ 35 <span style="color:blue">FIRSTSEQ</span>
≪ <span style="color:blue">SAFE?</span> ≫ 10000 <span style="color:blue">CNTSEQ</span>
≪ <span style="color:blue">SAFE?</span> NOT ≫ 40 <span style="color:blue">FIRSTSEQ</span>
≪ <span style="color:blue">SAFE?</span> NOT ≫ 10000 <span style="color:blue">CNTSEQ</span>
Counting safe numbers up to one million would take an hour, without really creating any opportunity to improve the algorithm or the code.
{{out}
<pre>
4: {5 7 11 23 47 59 83 107 167 179 227 263 347 359 383 467 479 503 563 587 719 839 863 887 983 1019 1187 1283 1307 1319 1367 1439 1487 1523 1619}
3: 115
2: {2 3 13 17 19 29 31 37 41 43 53 61 67 71 73 79 89 97 101 103 109 113 127 131 137 139 149 151 157 163 173 181 191 193 197 199 211 223 229 233}
1: 1114
</pre>
 
 
Line 3,038 ⟶ 3,253:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(1e7, false) // need primes up to 10 million here
1,983

edits