Safe and Sophie Germain primes: Difference between revisions

From Rosetta Code
Content added Content deleted
m (First 50)
(Added XPL0 example.)
Line 8: Line 8:
;Task
;Task
Generate the first   '''50'''   Sophie Germain prime numbers.
Generate the first   '''50'''   Sophie Germain prime numbers.

=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];

int N, Count;
[N:= 2;
Count:= 0;
repeat if IsPrime(N) & IsPrime(2*N+1) then
[IntOut(0, N); ChOut(0, 9\tab\);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0);
];
N:= N+1;
until Count >= 50;
]</lang>

{{out}}
<pre>
2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
359 419 431 443 491 509 593 641 653 659
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>

Revision as of 01:56, 10 December 2021

Safe and Sophie Germain primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A prime number p is Sophie Germain prime if 2p + 1 is also prime.

The number 2p + 1 associated with a Sophie Germain prime is called a safe prime.

Task

Generate the first   50   Sophie Germain prime numbers.

XPL0

<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number int N, I; [for I:= 2 to sqrt(N) do

   if rem(N/I) = 0 then return false;

return true; ];

int N, Count; [N:= 2; Count:= 0; repeat if IsPrime(N) & IsPrime(2*N+1) then

           [IntOut(0, N);  ChOut(0, 9\tab\);
           Count:= Count+1;
           if rem(Count/10) = 0 then CrLf(0);
           ];
       N:= N+1;

until Count >= 50; ]</lang>

Output:
2       3       5       11      23      29      41      53      83      89      
113     131     173     179     191     233     239     251     281     293     
359     419     431     443     491     509     593     641     653     659     
683     719     743     761     809     911     953     1013    1019    1031    
1049    1103    1223    1229    1289    1409    1439    1451    1481    1499