Giuga numbers: Difference between revisions
Content added Content deleted
(→{{header|Free Pascal}}: added version that generates prime factorication and than check Giuga) |
m (→alternative version: limit count of search factors to 7 and other small optimizations) |
||
Line 500: | Line 500: | ||
2*3 are set fixed. 2*3*5 followed 2*3*7 than 2*3*11. Therefor the results are unsorted. |
2*3 are set fixed. 2*3*5 followed 2*3*7 than 2*3*11. Therefor the results are unsorted. |
||
<lang pascal>program Giuga; |
<lang pascal>program Giuga; |
||
{ |
|||
30 = 2 * 3 * 5. |
|||
858 = 2 * 3 * 11 * 13. |
|||
1722 = 2 * 3 * 7 * 41. |
|||
66198 = 2 * 3 * 11 * 17 * 59. |
|||
2214408306 = 2 * 3 * 11 * 23 * 31 * 47057. |
|||
24423128562 = 2 * 3 * 7 * 43 * 3041 * 4447. |
|||
432749205173838 = 2 * 3 * 7 * 59 * 163 * 1381 * 775807. |
|||
14737133470010574 = 2 * 3 * 7 * 71 * 103 * 67213 * 713863. |
|||
550843391309130318 = 2 * 3 * 7 * 71 * 103 * 61559 * 29133437. |
|||
244197000982499715087866346 = 2 * 3 * 11 * 23 * 31 * 47137 * 28282147 * 3892535183. |
|||
554079914617070801288578559178 = 2 * 3 * 11 * 23 * 31 * 47059 * 2259696349 * 110725121051. |
|||
1910667181420507984555759916338506 = 2 * 3 * 7 * 43 * 1831 * 138683 * 2861051 * 1456230512169437. } |
|||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON} |
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON} |
||
Line 510: | Line 523: | ||
; |
; |
||
const |
const |
||
LMT = 24423128562;//24423128562;//2214408306;// |
LMT = 24423128562;//24423128562;//2214408306;//432749205173838 |
||
type |
type |
||
tFac = packed record |
tFac = packed record |
||
Line 517: | Line 530: | ||
fPrimIdx :Uint32; |
fPrimIdx :Uint32; |
||
end; |
end; |
||
tFacs = array[0.. |
tFacs = array[0..64] of tFac; |
||
tPrimes = array[0.. |
tPrimes = array[0..62157] of Uint32;//775807 |
||
// tPrimes = array[0.. |
// tPrimes = array[0..14379] of Uint32;//sqrt 24423128562 |
||
// tPrimes = array[0.. |
// tPrimes = array[0..1807414] of Uint32;//29133437 |
||
var |
var |
||
SmallPrimes: tPrimes; |
SmallPrimes: tPrimes; |
||
T0 : Int64; |
T0 : Int64; |
||
cnt:Uint32; |
cnt:Uint32; |
||
procedure InitSmallPrimes; |
procedure InitSmallPrimes; |
||
//get primes. |
//get primes. Sieving only odd numbers |
||
const |
const |
||
//MAXLIMIT = (trunc(sqrt(LMT)+1)-1) shr 1+4; |
|||
MAXLIMIT = (trunc(sqrt(LMT)+1)-1) shr 1; |
MAXLIMIT = 775807 DIV 2+1;//(trunc(sqrt(LMT)+1)-1) shr 1+4; |
||
var |
var |
||
pr : array of byte; |
pr : array of byte; |
||
Line 570: | Line 584: | ||
until (p > MAXLIMIT) OR (j>High(SmallPrimes)); |
until (p > MAXLIMIT) OR (j>High(SmallPrimes)); |
||
setlength(pr,0); |
setlength(pr,0); |
||
writeln(p); |
|||
writeln(j); |
|||
end; |
end; |
||
Line 606: | Line 618: | ||
procedure InsertNextPrimeFac(var F:tFacs;idx:Uint32); |
procedure InsertNextPrimeFac(var F:tFacs;idx:Uint32); |
||
var |
var |
||
Mul : Uint64; |
|||
i,p : uint32; |
|||
begin |
begin |
||
with F[idx-1] do |
|||
begin |
|||
Mul := fMul; |
|||
i := fPrimIdx+1; |
|||
end; |
|||
if i<High(SmallPrimes) then |
if i<High(SmallPrimes) then |
||
repeat |
repeat |
||
Line 614: | Line 631: | ||
begin |
begin |
||
p := SmallPrimes[i]; |
p := SmallPrimes[i]; |
||
⚫ | |||
fPrime := p; |
fPrime := p; |
||
fPrimIdx := i; |
|||
if p*Mul>LMT then |
|||
⚫ | |||
BREAK; |
BREAK; |
||
⚫ | |||
IF (Mul-1) MOD p = 0 then |
IF (Mul-1) MOD p = 0 then |
||
IF ChkGiuga(F,idx) then |
IF ChkGiuga(F,idx) then |
||
OutFac(F,idx); |
OutFac(F,idx); |
||
end; |
end; |
||
// max 7 factors [0..6] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
inc(i); |
inc(i); |
||
until i>High(SmallPrimes); |
until i>High(SmallPrimes); |
||
Line 644: | Line 662: | ||
fMul := 2*3;fPrime := 3;fPrimIdx:= 1; |
fMul := 2*3;fPrime := 3;fPrimIdx:= 1; |
||
end; |
end; |
||
InsertNextPrimeFac(Facs,2); |
InsertNextPrimeFac(Facs,2); |
||
Line 650: | Line 667: | ||
writeln('Found ',cnt); |
writeln('Found ',cnt); |
||
writeln; |
writeln; |
||
end. |
end. |
||
</lang> |
|||
{{out|@TIO.RUN}} |
{{out|@TIO.RUN}} |
||
<pre> |
<pre> |
||
78135 // limit of search prime |
|||
14380 // count of primes |
|||
1 2*3*5 = 30 0.000 s |
1 2*3*5 = 30 0.000 s |
||
2 2*3*7*41 = 1722 |
2 2*3*7*41 = 1722 1.921 s |
||
3 2*3*7*43*3041*4447 = 24423128562 |
3 2*3*7*43*3041*4447 = 24423128562 1.954 s |
||
4 2*3*11*13 = 858 |
4 2*3*11*13 = 858 2.527 s |
||
5 2*3*11*17*59 = 66198 |
5 2*3*11*17*59 = 66198 2.588 s |
||
6 2*3*11*23*31*47057 = 2214408306 |
6 2*3*11*23*31*47057 = 2214408306 2.645 s |
||
Found 6 |
Found 6 |
||
Real time: |
Real time: 8.733 s CPU share: 99.45 % |
||
//@home real |
//@home real 0m1,604s |
||
</pre> |
</pre> |
||