Additive primes: Difference between revisions
Added compatibility for Delphi
(add PicoLisp) |
MaiconSoft (talk | contribs) (Added compatibility for Delphi) |
||
Line 1,976:
<pre>%1 = [54, 54, 54, 54]</pre>
=={{header|Pascal}}==
{{works with|Free Pascal}}{{works with|Delphi}} checking isPrime(sum of digits) before testimg isprime(num) improves speed.<br>Tried to speed up calculation of sum of digits.
<lang pascal>program AdditivePrimes;
{$IFDEF FPC}
{$ELSE}
{$ENDIF}
{$DEFINE DO_OUTPUT}
uses
sysutils;
const
RANGE = 500; // 1000*1000;//
MAX_OFFSET = 0; // 1000*1000*1000;//
ColWidth = Trunc(ln(MAX_OFFSET+RANGE)/ln(10))+2;▼
MAXCOLUMNS = 80;▼
NextRowCnt = MAXCOLUMNS DIV ColWidth;▼
type
tNum = array [0 .. 15] of byte;
tNumSum = record
dgtNum, dgtSum: tNum;
dgtNum,▼
dgtLen,
tpNumSum = ^tNumSum;
function isPrime(n: Uint32): boolean;
const
wheeldiff
var
p: NativeUInt;
flipflop
begin
if n < 64 then
EXIT(n in [
else
begin
IF (n AND 1 = 0) OR (n mod 3 = 0
EXIT(false);
result := true;
Line 2,024 ⟶ 2,022:
while result do
Begin
p
if p * p > n then
BREAK;
result := n mod p <> 0;
flipflop
if flipflop < 0 then
flipflop := 7;
end
end
end;
procedure IncNum(var NumSum: tNumSum; delta: Uint32);
const
BASE = 10;
var
carry, dg: Uint32;
Begin
if delta = 0 then
Line 2,048 ⟶ 2,045:
with NumSum do
begin
num
repeat
carry := delta div BASE;
delta
dg := dgtNum[le] + delta;
IF dg >= BASE then
Begin
dg
inc(carry);
end;
Line 2,064 ⟶ 2,061:
if dgtLen < le then
dgtLen := le;
// correct sum of digits // le is >= 1
delta := dgtSum[le];
repeat
dec(le);
delta
dgtSum[le] := delta;
until le = 0;
end;
Line 2,076 ⟶ 2,073:
var
NumSum: tNumSum;
s
i, k, cnt, Nr:
ColWidth, MAXCOLUMNS, NextRowCnt: NativeUInt;
BEGIN
▲ ColWidth := Trunc(ln(MAX_OFFSET + RANGE) / ln(10)) + 2;
fillchar(NumSum,SizeOf(NumSum),#0);▼
▲ MAXCOLUMNS := 80;
▲ NextRowCnt := MAXCOLUMNS DIV ColWidth;
▲ fillchar(NumSum, SizeOf(NumSum), #0);
NumSum.dgtLen := 1;
IncNum(NumSum, MAX_OFFSET);
setlength(s, ColWidth);
fillchar(s[1], ColWidth, ' ');
// init string
with
Begin
For i :=
s[ColWidth - i] :=
// reset digits lenght to get the max changed digits since last update of string
end;
cnt := 0;
Nr := NextRowCnt;
For i := 0 to RANGE do
with NumSum do
begin
if
if
Begin
cnt
dec(
// correct changed digits in string s
For k :=
s[ColWidth - k] :=
{$IFDEF DO_OUTPUT}
write(s);
if
begin
writeln;
{$ENDIF}▼
end;
IncNum(NumSum, 1);▼
▲{$ENDIF}
▲ end;
▲ IncNum(NumSum,1);
end;
if
write(#10);
writeln(cnt, ' additive primes found.');
END.
</lang>
|