Chowla numbers: Difference between revisions
Added compatibility for Delphi
MaiconSoft (talk | contribs) m (Added Delphi reference to Pascal code) |
MaiconSoft (talk | contribs) (Added compatibility for Delphi) |
||
Line 2,308:
=={{header|Pascal}}==
{{works with|Free Pascal}}
{{works with|Delphi}}
{{trans|Go}} but not using a sieve, cause a sieve doesn't need precalculated small primes.<BR>
So runtime is as bad as trial division.
<lang pascal>program
{$IFDEF FPC}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
SysUtils
sysutils,strUtils{for Numb2USA};▼
{$IFDEF FPC}
{$ENDIF}
;
{$IFNDEF FPC}
function Chowla(n:NativeUint):NativeUint;▼
function Numb2USA(const S: string): string;
var
begin
I := Length(S);
Result := S;
NA := 0;
while (I > 0) do
begin
if ((Length(Result) - I + 1 - NA) mod 3 = 0) and (I <> 1) then
begin
Insert(',', Result, I);
Inc(NA);
end;
Dec(I);
end;
end;
{$ENDIF}
▲function Chowla(n: NativeUint): NativeUint;
var
Divisor, Quotient: NativeUint;
begin
result := 0;
Divisor := 2;
while sqr(Divisor) < n do
Quotient := n
inc(result, Divisor + Quotient);
inc(Divisor);
end;
inc(result, Divisor);
end;
procedure Count10Primes(Limit: NativeUInt);
var
n, i,
begin
writeln;
writeln(' primes til | count');
i := 100;
n := 2;
cnt := 0;
repeat
repeat
// Ord (true) = 1 ,Ord (false) = 0
inc(cnt, ORD(chowla(n) = 0));
inc(n);
until n > i;
writeln(Numb2USA(IntToStr(i)): 12, '|', Numb2USA(IntToStr(cnt)): 10);
i := i * 10;
until i > Limit;
end;
Line 2,356 ⟶ 2,386:
procedure CheckPerf;
var
k, kk, p, cnt,
begin
writeln;
writeln(' number that is perfect');
cnt := 0;
limit := 35000000;
k := 2;
kk := 3;
repeat
p := k * kk;
if p > limit then
BREAK;
if chowla(p) = (p - 1) then
writeln(Numb2USA(IntToStr(p)): 12);
inc(cnt);
end;
k := kk + 1;
inc(kk, k);
until false;
end;
var
begin
writeln('chowla(',
Count10Primes(10 * 1000 * 1000);
CheckPerf;
end.</lang>
|