Giuga numbers: Difference between revisions

m
syntax highlighting fixup automation
(added AWK)
m (syntax highlighting fixup automation)
Line 27:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f GIUGA_NUMBER.AWK
BEGIN {
Line 58:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 66:
===Brute force===
Based on the Go solution. Takes 26 minutes on my system (Intel Core i5 3.2GHz).
<langsyntaxhighlight lang="cpp">#include <iostream>
 
// Assumes n is even with exactly one factor of 2.
Line 97:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 112:
{{libheader|Boost}}
{{trans|Wren}}
<langsyntaxhighlight lang="cpp">#include <boost/rational.hpp>
 
#include <algorithm>
Line 200:
for (uint64_t n = 3; n < 7; ++n)
giuga_numbers(n);
}</langsyntaxhighlight>
 
{{out}}
Line 211:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Function isGiuga(m As Uinteger) As Boolean
Dim As Uinteger n = m
Dim As Uinteger f = 2, l = Sqr(n)
Line 231:
If isGiuga(n) Then c += 1: Print n; " ";
n += 1
Loop Until c = limit</langsyntaxhighlight>
{{out}}
<pre>The first 4 Giuga numbers are: 30 858 1722 66198</pre>
Line 238:
{{trans|Wren}}
I thought I'd see how long it would take to 'brute force' the fifth Giuga number and the answer (without using parallelization, Core i7) is about 1 hour 38 minutes.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 311:
fmt.Println("The first", limit, "Giuga numbers are:")
fmt.Println(giuga)
}</langsyntaxhighlight>
 
{{out}}
Line 323:
We can brute force this task building a test for giuga numbers and checking the first hundred thousand integers (which takes a small fraction of a second):
 
<langsyntaxhighlight Jlang="j">giguaP=: {{ (1<y)*(-.1 p:y)**/(=<.) y ((_1+%)%]) q: y }}"0</langsyntaxhighlight>
 
<langsyntaxhighlight Jlang="j"> 1+I.giguaP 1+i.1e5
30 858 1722 66198</langsyntaxhighlight>
 
These numbers have some interesting properties but there's an issue with guaranteeing correctness of more sophisticated approaches. That said, here's a translation of the pari/gp implementation on the talk page:
 
<langsyntaxhighlight Jlang="j">divisors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
 
giuga=: {{
Line 368:
end.
r
}}</langsyntaxhighlight>
 
Example use:<langsyntaxhighlight Jlang="j"> giuga 1
 
giuga 2
Line 381:
66198
giuga 6
24423128562 2214408306</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="ruby">using Primes
 
isGiuga(n) = all(f -> f != n && rem(n ÷ f - 1, f) == 0, factor(Vector, n))
Line 399:
 
getGiuga(4)
</langsyntaxhighlight>{{out}}
<pre>
30
Line 407:
</pre>
=== Ad hoc faster version ===
<langsyntaxhighlight lang="ruby">using Primes
 
function getgiugas(numberwanted, verbose = true)
Line 429:
@time getgiugas(2, false)
@time getgiugas(6)
</langsyntaxhighlight>{{out}}
<pre>
30 (elapsed: 0.0)
Line 445:
That means always factors 2,3 and minimum one of 5,7,11.<br>
<br>
<langsyntaxhighlight lang="pascal">program Giuga;
 
{$IFDEF FPC}
Line 673:
writeln;
writeln(OutPots(@PrimeDecomp,n));
end.</langsyntaxhighlight>
{{out|@home AMD 5600G ( 4,4 Ghz ) fpc3.2.2 -O4 -Xs}}
<pre>
Line 691:
Generating recursive squarefree numbers of ascending primes and check those numbers.<BR>
2*3 are set fixed. 2*3*5 followed 2*3*7 than 2*3*11. Therefor the results are unsorted.
<langsyntaxhighlight lang="pascal">program Giuga;
{
30 = 2 * 3 * 5.
Line 889:
writeln;
end.
</syntaxhighlight>
</lang>
{{out|@TIO.RUN}}
<pre>
Line 934:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Giuga_numbers
Line 945:
my $n = $_;
all { ($n / $_ - 1) % $_ == 0 } factor $n and print "$n\n";
} 4, 67000;</langsyntaxhighlight>
{{out}}
<pre>
Line 955:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span>
Line 969:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d Giuga numbers are: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">giuga</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 976:
===Faster version===
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Giuga_number.exw
Line 1,032:
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span><span style="color: #000000;">giuga</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,041:
</pre>
You can (almost) push things a little further on 64-bit:
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span> <span style="color: #008080;">then</span> <span style="color: #000000;">giuga</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
and get
<pre>
Line 1,052:
=={{header|Python}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
from math import sqrt
Line 1,081:
c += 1
print(n)
n += 1</langsyntaxhighlight>
 
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my @primes = (3..60).grep: &is-prime;
 
print 'First four Giuga numbers: ';
Line 1,095:
$n if all .map: { ($n / $_ - 1) %% $_ };
}
}</langsyntaxhighlight>
{{out}}
<pre>First 4 Giuga numbers: 30 858 1722 66198</pre>
Line 1,104:
 
Takes only about 0.05 seconds to find the first four Giuga numbers but finding the fifth would take many hours using this approach, so I haven't bothered.
<langsyntaxhighlight lang="ecmascript">var factors = []
var inc = [4, 2, 4, 2, 4, 6, 2, 6]
 
Line 1,163:
}
System.print("The first %(limit) Giuga numbers are:")
System.print(giuga)</langsyntaxhighlight>
 
{{out}}
Line 1,175:
{{libheader|Wren-rat}}
This is a translation of the very fast Pari-GP code in the talk page. Only takes 0.015 seconds to find the first six Giuga numbers.
<langsyntaxhighlight lang="ecmascript">import "./math" for Math, Int
import "./rat" for Rat
 
Line 1,218:
giuga.call(n)
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,238:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Giuga(N0); \Return 'true' if Giuga number
int N0;
int N, F, Q1, Q2, L;
Line 1,265:
N:= N+1;
];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits