Chowla numbers: Difference between revisions

Added compatibility for Delphi
m (Added Delphi reference to Pascal code)
(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 ChowlaChowla_numbers;
 
{$IFDEF FPC}
{$MODE Delphi}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
 
uses
SysUtils
sysutils,strUtils{for Numb2USA};
{$IFDEF FPC}
sysutils ,strUtilsStrUtils{for Numb2USA};
{$ENDIF}
;
 
 
{$IFNDEF FPC}
function Chowla(n:NativeUint):NativeUint;
function Numb2USA(const S: string): string;
var
DivisorI,Quotient NA: NativeUintInteger;
begin
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
Beginbegin
Quotient := n DIVdiv Divisor;
IFif Quotient * Divisor = n then
inc(result, Divisor + Quotient);
inc(Divisor);
end;
IFif sqr(Divisor) = n then
inc(result, Divisor);
end;
 
procedure Count10Primes(Limit: NativeUInt);
var
n, i,cnt cnt: integer;
begin
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,limit limit: NativeInt;
begin
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
Beginbegin
writeln(Numb2USA(IntToStr(p)): 12);
inc(cnt);
end;
k := kk + 1;
inc(kk, k);
until false;
end;
 
var
i I: integer;
 
Begin
begin
Forfor iI := 2 to 37 do
writeln('chowla(',i I: 2, ') =', chowla(iI): 3);
Count10Primes(10 * 1000 * 1000);
CheckPerf;
end.</lang>
478

edits