Additive primes: Difference between revisions

no edit summary
(Add VTL-2)
No edit summary
Line 1,565:
467 17
487 19</pre>
 
=={{header|Free Pascal}}==
Using Sieve of Eratosthenes to find all primes upto 500,
then go through the list, sum digits and check for prime
 
<lang pascal>
Program AdditivePrimes;
Const max_number = 500;
 
Var is_prime : array Of Boolean;
 
Procedure sieve(Var arr: Array Of boolean );
{use Sieve of Eratosthenes to find all primes to max number}
Var i,j : NativeUInt;
 
Begin
For i := 2 To high(arr) Do
arr[i] := True; // set all bits to be True
For i := 2 To high(arr) Do
Begin
If (arr[i]) Then
For j := 2 To (high(arr) Div i) Do
arr[i * j] := False;
End;
End;
 
Function GetSumOfDigits(num: NativeUInt): longint;
{calcualte the sum of digits of a number}
Var
sum : longint = 0;
dummy: NativeUInt;
Begin
Repeat
dummy := num;
num := num Div 10;
Inc(sum, dummy - (num SHL 3 + num SHL 1));
Until num < 1;
GetSumOfDigits := sum;
End;
 
Var x : NativeUInt = 2; {first prime}
counter : longint = 0;
Begin
setlength(is_prime,max_number); //set length of array to max_number
Sieve(is_prime); //apply Sieve
{since 2 is the only even prime, let's do it separate}
If is_prime[x] And is_prime[GetSumOfDigits(x)] Then
Begin
write(x,' ');
inc(counter);
End;
inc(x);
While x < max_number Do
Begin
If is_prime[x] And is_prime[GetSumOfDigits(x)] Then
Begin
write(x,' ');
inc(counter);
End;
inc(x,2);
End;
writeln();
writeln();
writeln(counter,' additive primes found.');
End.
</lang>
{{out}}
<pre>
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157
173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317
331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
 
54 additive primes found.
</pre>
 
 
 
=={{header|Go}}==
45

edits