Unprimeable numbers: Difference between revisions

m
Fix pascal version to run in delphi
m (Minor edit to C++ code)
m (Fix pascal version to run in delphi)
Line 1,074:
 
=={{header|Pascal}}==
{{trans|Go}} {{works with|Free Pascal}}{{works with|Delphi}}
Small improvement.When the check of value ending in "0" is not unprimable than I can jump over by 10, since the check already has checked those numbers ending in "1".."9".But in case of unprimable I am using a reduced version of the check<BR>Results in runtime reduced from 1.8 secs downto 0.667 now to 0.46
<lang pascal>program unprimable;
{$IFDEF FPC}{$Mode Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
 
const
base = 10;
 
type
TNumVal = array[0..base-1] of NativeUint;
Line 1,089:
MaxIdx : NativeUint;
end;
 
var //global
PotBase,
Line 1,095:
TotalCnt,
EndDgtCnt :NativeUint;
 
procedure Init;
var
Line 1,110:
EndDgtCnt := 0;
end;
 
Procedure ConvertNum(n: NativeUint;var NConv:TConvNum);
//extract digit position replace by "0" to get NumRest
Line 1,133:
end;
end;
 
procedure CheckOutPut(n: NativeUint);
Begin
Line 1,147:
end;
end;
 
function isPrime(n : NativeUint):boolean;inline;
var
Line 1,163:
if n mod p = 0 then
Exit;
inc(p += ,2);
if n mod p = 0 then
Exit;
inc(p += ,4);
end;
result := true;
end;
 
procedure InsertFound(LowDgt,n:NativeUInt);
Begin
Line 1,180:
end;
end;
 
function CheckUnprimable(n:NativeInt):boolean;
var
Line 1,196:
EXIT;
dgt := LowDgt;
 
result := true;
i := MaxIdx;
Line 1,208:
end;
end;
 
result := false;
For i := MaxIdx downto 1 do
Line 1,225:
end;
end;
 
function CheckUnprimableReduced(n:NativeInt):boolean;
//lowest digit already tested before
Line 1,246:
end;
end;
 
result := false;
For i := i downto 1 do
Line 1,263:
end;
end;
 
var
n,i : NativeUint;
Line 1,286:
writeln;
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
{$IFNDEF UNIX}readln;{$ENDIF}
end.</lang>
{{out}}
478

edits