Achilles numbers: Difference between revisions

 
(23 intermediate revisions by 13 users not shown)
Line 48:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program achilleNumber.s */
Line 764:
/***************************************************/
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
First 50 Achilles Numbers:
Line 785:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find Achilles Numbers: numbers whose prime factors p appear at least #
# twice (i.e. if p is a prime factor, so is p^2) and cannot be #
# expressed as m^k for any integer m, k > 1 #
Line 901:
OD
END
END</langsyntaxhighlight>
{{out}}
<pre>First 50 Achilles Numbers:
Line 919:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program achilleNumber.s */
Line 1,546:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
First 50 Achilles Numbers:
Line 1,563:
Numbers with 6 digits : 664
</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">function GCD(n, d)
if(d = 0) then return n else return GCD(d, n % d)
end function
 
function Totient(n)
tot = 0
for m = 1 to n
if GCD(m, n) = 1 then tot += 1
next m
return tot
end function
 
function isPowerful(m)
n = m
f = 2
l = sqr(m)
 
if m <= 1 then return false
while true
q = n/f
if (n % f) = 0 then
if (m %(f*f)) then return false
n = q
if f > n then exit while
else
f += 1
if f > l then
if (m % (n*n)) then return false
exit while
end if
end if
end while
return true
end function
 
function isAchilles(n)
if not isPowerful(n) then return false
m = 2
a = m*m
do
do
if a = n then return false
a *= m
until a > n
m += 1
a = m*m
until a > n
return true
end function
 
print "First 50 Achilles numbers:"
num = 0
n = 1
do
if isAchilles(n) then
print rjust(n, 5);
num += 1
if (num % 10) <> 0 then print " "; else print
end if
n += 1
until num >= 50
 
print : print
print "First 20 strong Achilles numbers:"
num = 0
n = 1
do
if isAchilles(n) and isAchilles(Totient(n)) then
print rjust(n, 5);
num += 1
if (num % 10) <> 0 then print " "; else print
end if
n += 1
until num >= 20
 
print : print
print "Number of Achilles numbers with:"
for i = 2 to 6
inicio = fix(10.0 ^ (i-1))
num = 0
for n = inicio to inicio*10-1
if isAchilles(n) then num += 1
next n
print i; " digits:"; num
next i</syntaxhighlight>
 
 
 
[[https://dotnetfiddle.net/TCD2WC You may run it online!]]
 
=={{header|C++}}==
{{trans|Wren}}
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <chrono>
#include <cmath>
Line 1,658 ⟶ 1,751:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 1,695 ⟶ 1,788:
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
function GetTotient(N: integer): integer;
{Calculate Euler's Totient}
var M: integer;
begin
Result:= 0;
for M:= 1 to N do
if GreatestCommonDivisor(M, N) = 1 then
Result:= Result+1;
end;
 
 
function IsPowerfulNum(N: integer): boolean;
{Is a powerful number i.e. all prime factors square are divisor}
var I: integer;
var IA: TIntegerDynArray;
begin
Result:=False;
GetPrimeFactors(N,IA);
for I:=0 to High(IA) do
if (N mod (IA[I]*IA[I]))<>0 then exit;
Result:=True;
end;
 
 
function CanBeMtoK(N: integer): boolean;
{Can N be represented as M^K?}
var M, A: integer;
begin
Result:=False;
M:= 2;
A:= M*M;
repeat
begin
while true do
begin
if A = N then exit;
if A > N then break;
A:= A*M;
end;
M:= M+1;
A:= M*M;
end
until A > N;
Result:=True;
end;
 
 
function IsAchilles(N: integer): boolean;
{Achilles = Is Powerful and can be M^K}
begin
Result:=IsPowerfulNum(N) and CanBeMtoK(N);
end;
 
 
 
procedure AchillesNumbers(Memo: TMemo);
var I,Cnt,Digits: integer;
var S: string;
var DigCnt: array [0..5] of integer;
begin
Memo.Lines.Add('First 50 Achilles numbers:');
Cnt:=0; S:='';
for I:=2 to high(Integer) do
if IsAchilles(I) then
begin
Inc(Cnt);
S:=S+Format('%6d',[I]);
if (Cnt mod 10)=0 then S:=S+CRLF;
if Cnt>=50 then break;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('First 20 Strong Achilles Numbers:');
Cnt:=0; S:='';
for I:=2 to high(Integer) do
if IsAchilles(I) then
if IsAchilles(GetTotient(I)) then
begin
Inc(Cnt);
S:=S+Format('%6d',[I]);
if (Cnt mod 10)=0 then S:=S+CRLF;
if Cnt>=20 then break;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('Digits Counts:');
for I:=0 to High(DigCnt) do DigCnt[I]:=0;
for I:=2 to high(Integer) do
begin
Digits:=NumberOfDigits(I);
if Digits>High(DigCnt) then break;
if IsAchilles(I) then Inc(DigCnt[Digits]);
end;
Memo.Lines.Add('Last Count: '+IntToStr(I));
for I:=0 to High(DigCnt) do
if DigCnt[I]>0 then
begin
Memo.Lines.Add(Format('%d digits: %d',[I,DigCnt[I]]));
end
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 20 Strong Achilles Numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
 
Digits Counts:
Last Count: 100000
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
func gcd n d .
if d = 0
return n
.
return gcd d (n mod d)
.
func totient n .
for m = 1 to n
if gcd m n = 1
tot += 1
.
.
return tot
.
func isPowerful m .
n = m
f = 2
l = sqrt m
if m <= 1
return 0
.
while 1 = 1
q = n div f
if n mod f = 0
if m mod (f * f) <> 0
return 0
.
n = q
if f > n
return 1
.
else
f += 1
if f > l
if m mod (n * n) <> 0
return 0
.
return 1
.
.
.
.
func isAchilles n .
if isPowerful n = 0
return 0
.
m = 2
a = m * m
repeat
repeat
if a = n
return 0
.
a *= m
until a > n
.
m += 1
a = m * m
until a > n
.
return 1
.
print "First 50 Achilles numbers:"
n = 1
repeat
if isAchilles n = 1
write n & " "
num += 1
.
n += 1
until num >= 50
.
print ""
print ""
print "First 20 strong Achilles numbers:"
num = 0
n = 1
repeat
if isAchilles n = 1 and isAchilles totient n = 1
write n & " "
num += 1
.
n += 1
until num >= 20
.
print ""
print ""
print "Number of Achilles numbers with 2 to 5 digits:"
a = 10
b = 100
for i = 2 to 5
num = 0
for n = a to b - 1
if isAchilles n = 1
num += 1
.
.
write num & " "
a = b
b *= 10
.
</syntaxhighlight>
 
{{out}}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800 864 968 972 1125 1152 1323 1352 1372 1568 1800 1944 2000 2312 2592 2700 2888 3087 3200 3267 3456 3528 3872 3888 4000 4232 4500 4563 4608 5000 5292 5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 20 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348 12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
 
Number of Achilles numbers with 2 to 5 digits:
1 12 47 192
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<syntaxhighlight lang="factor">USING: assocs combinators.short-circuit formatting grouping io
kernel lists lists.lazy math math.functions math.primes.factors
prettyprint ranges sequences ;
 
: achilles? ( n -- ? )
group-factors values {
[ [ 1 > ] all? ]
[ unclip-slice [ simple-gcd ] reduce 1 = ]
} 1&& ;
 
: achilles ( -- list )
2 lfrom [ achilles? ] lfilter ;
 
: strong-achilles ( -- list )
achilles [ totient achilles? ] lfilter ;
 
: show ( n list -- ) ltake list>array 10 group simple-table. ;
 
: <order-of-magnitude> ( n -- range )
1 - 10^ dup 10 * [a..b) ;
 
"First 50 Achilles numbers:" print
50 achilles show nl
 
"First 30 strong Achilles numbers:" print
30 strong-achilles show nl
 
"Number of Achilles numbers with" print
{ 2 3 4 5 } [
dup <order-of-magnitude> [ achilles? ] count
"%d digits: %d\n" printf
] each</syntaxhighlight>
{{out}}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 30 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
 
Number of Achilles numbers with
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Function GCD(n As Uinteger, d As Uinteger) As Uinteger
Return Iif(d = 0, n, GCD(d, n Mod d))
End Function
Line 1,784 ⟶ 2,184:
Print i; " digits:"; num
Next i
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 50 Achilles numbers:
Line 1,808 ⟶ 2,208:
{{trans|Wren}}
Based on Version 2, takes around 19 seconds.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,917 ⟶ 2,317:
fmt.Printf("%2d digits: %d\n", d, ac)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,953 ⟶ 2,353:
Implementation:
 
<langsyntaxhighlight Jlang="j">achilles=: (*/ .>&1 * 1 = +./)@(1{__&q:)"0
strong=: achilles@(5&p:)</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> 5 10$(#~ achilles) 1+i.10000 NB. first 50 achilles numbers
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
Line 1,977 ⟶ 2,377:
192
+/achilles (+i.)/1 9*10^<:6
664</langsyntaxhighlight>
 
Explanation of the code:
Line 1,992 ⟶ 2,392:
 
<tt>(#~ predicate) list</tt> selects the elements of <tt>list</tt> where <tt>predicate</tt> is true.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class AchillesNumbers {
 
private Map<Integer, Boolean> pps = new HashMap<>();
 
public int totient(int n) {
int tot = n;
int i = 2;
while (i * i <= n) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
tot -= tot / i;
}
if (i == 2) {
i = 1;
}
i += 2;
}
if (n > 1) {
tot -= tot / n;
}
return tot;
}
 
public void getPerfectPowers(int maxExp) {
double upper = Math.pow(10, maxExp);
for (int i = 2; i <= Math.sqrt(upper); i++) {
double fi = i;
double p = fi;
while (true) {
p *= fi;
if (p >= upper) {
break;
}
pps.put((int) p, true);
}
}
}
 
public Map<Integer, Boolean> getAchilles(int minExp, int maxExp) {
double lower = Math.pow(10, minExp);
double upper = Math.pow(10, maxExp);
Map<Integer, Boolean> achilles = new HashMap<>();
for (int b = 1; b <= (int) Math.cbrt(upper); b++) {
int b3 = b * b * b;
for (int a = 1; a <= (int) Math.sqrt(upper); a++) {
int p = b3 * a * a;
if (p >= (int) upper) {
break;
}
if (p >= (int) lower) {
if (!pps.containsKey(p)) {
achilles.put(p, true);
}
}
}
}
return achilles;
}
 
public static void main(String[] args) {
AchillesNumbers an = new AchillesNumbers();
 
int maxDigits = 8;
an.getPerfectPowers(maxDigits);
Map<Integer, Boolean> achillesSet = an.getAchilles(1, 5);
List<Integer> achilles = new ArrayList<>(achillesSet.keySet());
Collections.sort(achilles);
 
System.out.println("First 50 Achilles numbers:");
for (int i = 0; i < 50; i++) {
System.out.printf("%4d ", achilles.get(i));
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
 
System.out.println("\nFirst 30 strong Achilles numbers:");
List<Integer> strongAchilles = new ArrayList<>();
int count = 0;
for (int n = 0; count < 30; n++) {
int tot = an.totient(achilles.get(n));
if (achillesSet.containsKey(tot)) {
strongAchilles.add(achilles.get(n));
count++;
}
}
for (int i = 0; i < 30; i++) {
System.out.printf("%5d ", strongAchilles.get(i));
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
 
System.out.println("\nNumber of Achilles numbers with:");
for (int d = 2; d <= maxDigits; d++) {
int ac = an.getAchilles(d - 1, d).size();
System.out.printf("%2d digits: %d\n", d, ac);
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 30 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
 
Number of Achilles numbers with:
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
6 digits: 664
7 digits: 2242
8 digits: 7395
 
</pre>
 
=={{header|jq}}==
'''Adapted from the "fast" [[#Wren|Wren]] entry'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
`nwise` is included below because, as of this writing, gojq does not include `_nwise`.
<syntaxhighlight lang="jq">
# Require $n > 0
def nwise($n):
def _n: if length <= $n then . else .[:$n] , (.[$n:] | _n) end;
if $n <= 0 then "nwise: argument should be non-negative" else _n end;
 
### Part 1 - generic functions
 
# Ensure $x is in the input sorted array
def ensure($x):
bsearch($x) as $i
| if $i >= 0 then .
else (-1-$i) as $i
| .[:$i] + [$x] + .[$i:]
end ;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
 
def table($wide; $pad):
nwise($wide) | map(lpad($pad)) | join(" ");
 
def count(s): reduce s as $x (0; .+1);
 
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# jq optimizes the recursive call of _gcd in the following:
def gcd($a;$b):
def _gcd:
if .[1] != 0 then [.[1], .[0] % .[1]] | _gcd else .[0] end;
[$a,$b] | _gcd ;
 
def totient:
. as $n
| count( range(0; .) | select( gcd($n; .) == 1) );
 
# Emit a sorted array
def getPerfectPowers( $maxExp ):
(10 | power($maxExp)) as $upper
| reduce range( 2; 1 + ($upper|sqrt|floor)) as $i ({pps: []};
.p = $i * $i
| until (.p >= $upper;
.pps += [ .p ]
| .p *= $i) )
| .pps
| sort;
 
# Input: a sufficiently long array of perfect powers in order
def getAchilles($minExp; $maxExp):
def cbrt: pow(.; 1/3);
. as $pps
| (10 | power($minExp)) as $lower
| (10 | power($maxExp)) as $upper
| ($upper | sqrt | floor) as $sqrtupper
| reduce range(1; 1 + ($upper|cbrt|floor)) as $b ({achilles: []};
($b | .*.*.) as $b3
| .done = false
| .a = 1
| until(.done or (.a > $sqrtupper);
($b3 * .a * .a) as $p
| if $p >= $upper then .done = true
elif $p >= $lower and ($pps | bsearch($p) < 0)
then .achilles |= ensure($p)
end
| .a += 1 ) )
| .achilles;
 
def task($maxDigits):
getPerfectPowers($maxDigits)
| . as $perfectPowers
| getAchilles(1; 5)
| . as $achilles
| "First 50 Achilles numbers:",
(.[:50] | table(10;5)),
"\nFirst 30 strong Achilles numbers:",
({ strongAchilles:[], count:0, n:0 }
| until (.count >= 30;
$achilles[.n] as $a
| ($a | totient) as $tot
| if ($achilles | bsearch($tot)) >= 0
then .strongAchilles |= ensure($a)
| .count += 1
end
| .n += 1 )
| (.strongAchilles | table(10;5) ),
"\nNumber of Achilles numbers with:",
( range(2; 1+$maxDigits) as $d
| ($perfectPowers|getAchilles($d-1; $d)|length) as $ac
| "\($d) digits: \($ac)" ) )
;
 
task(10)
</syntaxhighlight>
{{output}}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 30 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
 
Number of Achilles numbers with:
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
6 digits: 664
7 digits: 2242
8 digits: 7395
9 digits: 24008
10 digits: 77330
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="text">using Primes
 
isAchilles(n) = (p = [x[2] for x in factor(n).pe]; all(>(1), p) && gcd(p) == 1)
Line 2,030 ⟶ 2,692:
 
teststrongachilles()
</langsyntaxhighlight>{{out}}
<pre>
First 50 Achilles numbers:
Line 2,060 ⟶ 2,722:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[PowerfulNumberQ, StrongAchillesNumberQ]
PowerfulNumberQ[n_Integer] := AllTrue[FactorInteger[n][[All, 2]], GreaterEqualThan[2]]
AchillesNumberQ[n_Integer] := Module[{divs},
Line 2,091 ⟶ 2,753:
]][[2, 1]]
 
Tally[IntegerLength /@ Select[Range[9999999], AchillesNumberQ]] // Grid</langsyntaxhighlight>
{{out}}
<pre>{72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125, 1152, 1323, 1352, 1372, 1568, 1800, 1944, 2000, 2312, 2592, 2700, 2888, 3087, 3200, 3267, 3456, 3528, 3872, 3888, 4000, 4232, 4500, 4563, 4608, 5000, 5292, 5324, 5400, 5408, 5488, 6075, 6125, 6272, 6728, 6912, 7200}
Line 2,103 ⟶ 2,765:
6 664
7 2242</pre>
 
=={{header|Nim}}==
{{trans|Wren}}
<syntaxhighlight lang="Nim">import std/[algorithm, sets, math, sequtils, strformat, strutils]
 
const MaxDigits = 15
 
func getPerfectPowers(maxExp: int): HashSet[int] =
let upper = 10^maxExp
for i in 2..int(sqrt(upper.toFloat)):
var p = i
while p < upper div i:
p *= i
result.incl p
 
let pps = getPerfectPowers(MaxDigits)
 
proc getAchilles(minExp, maxExp: int): HashSet[int] =
let lower = 10^minExp
let upper = 10^maxExp
for b in 1..int(cbrt(upper.toFloat)):
let b3 = b * b * b
for a in 1..int(sqrt(upper.toFloat)):
let p = b3 * a * a
if p >= upper: break
if p >= lower:
if p notin pps: result.incl p
 
 
### Part 1 ###
 
let achillesSet = getAchilles(1, 6)
let achilles = sorted(achillesSet.toSeq)
 
echo "First 50 Achilles numbers:"
for i in 0..49:
let n = achilles[i]
stdout.write &"{n:>4}"
stdout.write if i mod 10 == 9: '\n' else: ' '
 
 
### Part 2 ###
 
func totient(n: int): int =
var n = n
result = n
var i = 2
while i * i <= n:
if n mod i == 0:
while n mod i == 0:
n = int(n / i)
result -= int(result / i)
if i == 2: i = 1
inc i, 2
if n > 1:
result -= int(result / n)
 
echo "\nFirst 50 strong Achilles numbers:"
var strongAchilles: seq[int]
var count = 0
for n in achilles:
let tot = totient(n)
if tot in achillesSet:
strongAchilles.add n
inc count
if count == 50: break
 
for i, n in strongAchilles:
stdout.write &"{n:>6}"
stdout.write if i mod 10 == 9: '\n' else: ' '
 
 
### Part 3 ###
 
echo "\nNumber of Achilles numbers with:"
for d in 2..MaxDigits:
let ac = getAchilles(d - 1, d).len
echo &"{d:>2} digits: {ac}"
</syntaxhighlight>
{{out}}
<pre>First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 50 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
81000 83349 84375 93312 108000 111132 124416 128000 135000 148176
151875 158184 162000 165888 172872 177957 197568 200000 202612 209952
 
Number of Achilles numbers with:
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
6 digits: 664
7 digits: 2242
8 digits: 7395
9 digits: 24008
10 digits: 77330
11 digits: 247449
12 digits: 788855
13 digits: 2508051
14 digits: 7960336
15 digits: 25235383
</pre>
 
=={{header|Perl}}==
Borrowed, and lightly modified, code from [[Powerful_numbers]]
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say current_sub>;
use experimental 'signatures';
use List::AllUtils <max head uniqint>;
use ntheory <is_square_free is_power euler_phi>;
use Math::AnyNum <:overload idiv is_power iroot ipow is_coprime>;
 
sub table { my $t = shift() * (my $c = 1 + length max @_); ( sprintf( ('%' . $c . 'd') x @_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
sub powerful_numbers ($n, $k = 2) {
Line 2,125 ⟶ 2,897:
__SUB__->($m * ipow($v, $r), $r - 1);
}
}->(1, 2 * $k - 1);
sort { $a <=> $b } @powerful;
}
 
my(@P, (@achilles, %Ahash, @strong);
my @P = uniqint @P, powerful_numbers(10**9, $_2) for 2..9; shift @P;
!is_power($_) and push @achilles, $_ and $Ahash{$_}++ for @P;
$Ahash{euler_phi $_} and push @strong, $_ for @achilles;
 
say "First 50 Achilles numbers:\n" . table 10, . table 10, head 50, @achilles;
say "First 30 strong Achilles numbers:\n" . table 10, head 30, @strong;
say "Number of Achilles numbers with:\n";
 
for my $l (2..9) {
for my $c; $l ==(2 length.. and $c++ for9) @achilles;{
my $c;
$l == length and $c++ for @achilles;
say "$l digits: $c";
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Achilles numbers:
Line 2,164 ⟶ 2,938:
9 digits: 24008</pre>
Here is a translation from Wren version 2, as an alternative.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,232 ⟶ 3,006:
for my $d (2..$maxDigits) {
printf "%2d digits: %d\n", $d, scalar getAchilles($d-1, $d)
}</langsyntaxhighlight>
Output is the same.
 
Line 2,239 ⟶ 3,013:
You can run this online [http://phix.x10.mx/p2js/achilles.htm here], though [slightly outdated and] you should expect a blank screen for about 9s.
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- [join_by(fmt)]</span>
Line 2,300 ⟶ 3,074:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,330 ⟶ 3,104:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from math import gcd
from sympy import factorint
Line 2,367 ⟶ 3,141:
 
test_strong_Achilles(50, 100)
</langsyntaxhighlight>{{out}}
<pre>
First 50 Achilles numbers:
Line 2,394 ⟶ 3,168:
[10000, 99999]: 192
[100000, 999999]: 664
</pre>
 
=={{header|Quackery}}==
 
<code>gcd</code> is defined at [[Greatest common divisor#Quackery]].
 
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
 
<code>totient</code> is defined at [[Totient function#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ ' [ 1 ] swap
behead swap witheach
[ dup dip
[ = iff
[ -1 pluck
1+ join ]
else
[ 1 join ] ] ]
drop ] is runs ( [ --> [ )
 
[ 1 over find swap found not ] is powerful ( [ --> b )
 
[ behead swap witheach gcd
1 = ] is imperfect ( [ --> b )
 
[ dup 2 < iff
[ drop false ] done
primefactors runs
dup powerful iff
imperfect
else [ drop false ] ] is achilles ( [ --> b )
 
[ dup achilles iff
[ totient achilles ]
else [ drop false ] ] is strong ( [ --> b )
 
[] 0
[ 1+ dup achilles if
[ tuck join swap ]
over size 50 = until ]
drop
say "First fifty achilles numbers:" cr
echo
cr cr
[] 0
[ 1+ dup strong if
[ tuck join swap ]
over size 20 = until ]
drop
say "First twenty strong achilles numbers:" cr
echo
cr cr
0 100 times
[ i^ achilles if 1+ ]
say "Achilles numbers with 2 digits: " echo
cr
0 900 times
[ i^ 100 + achilles if 1+ ]
say "Achilles numbers with 3 digits: " echo
cr
0 9000 times
[ i^ 1000 + achilles if 1+ ]
say "Achilles numbers with 4 digits: " echo
cr
0 90000 times
[ i^ 10000 + achilles if 1+ ]
say "Achilles numbers with 5 digits: " echo
cr
</syntaxhighlight>
 
{{out}}
 
<pre>First fifty achilles numbers:
[ 72 108 200 288 392 432 500 648 675 800 864 968 972 1125 1152 1323 1352 1372 1568 1800 1944 2000 2312 2592 2700 2888 3087 3200 3267 3456 3528 3872 3888 4000 4232 4500 4563 4608 5000 5292 5324 5400 5408 5488 6075 6125 6272 6728 6912 7200 ]
 
First twenty strong achilles numbers:
[ 500 864 1944 2000 2592 3456 5000 10125 10368 12348 12500 16875 19652 19773 30375 31104 32000 33275 37044 40500 ]
 
Achilles numbers with 2 digits: 1
Achilles numbers with 3 digits: 12
Achilles numbers with 4 digits: 47
Achilles numbers with 5 digits: 192
</pre>
 
=={{header|Raku}}==
Timing is going to be system / OS dependent.
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
use Math::Root;
 
Line 2,443 ⟶ 3,299:
say "$_ digits: " ~ +$achilles{$_} // 0 for 2..9;
 
printf "\n%.1f total elapsed seconds\n", now - INIT now;</langsyntaxhighlight>
{{out}}
<pre>First 50 Achilles numbers:
Line 2,475 ⟶ 3,331:
 
410.4 total elapsed seconds
</pre>
 
=={{header|RPL}}==
Based on Wikipedia definition: n = p<sub>1</sub><sup>a<sub>1</sub></sup>p<sub>2</sub><sup>a<sub>2</sub></sup>…p<sub>k</sub><sup>a<sub>k</sub></sup> is an Achilles number if min(a<sub>1</sub>, a<sub>2</sub>, …, a<sub>k</sub>) ≥ 2 and gcd(a<sub>1</sub>, a<sub>2</sub>, …, a<sub>k</sub>) = 1.
{{works with|HP|49g}}
≪ FACTORS
'''IF''' DUP SIZE 4 < '''THEN''' DROP 0
'''ELSE'''
{ } 2 3 PICK SIZE '''FOR''' j <span style="color:grey">@ keeps exponents only</span>
OVER j GET +
2 '''STEP''' NIP
→ a
≪ a ≪ MIN ≫ STREAM 2 ≥ a ≪ GCD ≫ STREAM 1 == AND
'''END'''
≫ '<span style="color:blue">ACH?</span>' STO
≪ { } 1 → n
≪ '''WHILE''' DUP SIZE 50 < '''REPEAT'''
'''IF''' 'n' INCR <span style="color:blue">ACH?</span> '''THEN''' n + '''END'''
'''END'''
≫ ≫ EVAL
≪ { } 1 → n
≪ '''WHILE''' DUP SIZE 20 < '''REPEAT'''
'''IF''' 'n' INCR <span style="color:blue">ACH?</span>
'''THEN''' IF n EULER <span style="color:blue">ACH?</span> '''THEN''' n + '''END END'''
'''END'''
≫ ≫ EVAL
{{out}}
<pre>
2: {72 108 200 288 392 432 500 648 675 800 864 968 972 1125 1152 1323 1352 1372 1568 1800 1944 2000 2312 2592 2700 2888 3087 3200 3267 3456 3528 3872 3888 4000 4232 4500 4563 4608 5000 5292 5324 5400 5408 5488 6075 6125 6272 6728 6912 7200}
1: {500 864 1944 2000 2592 3456 5000 10125 10368 12348 12500 16875 19652 19773 30375 31104 32000 33275 37044 40500}
</pre>
First 50 Achilles numbers found in 53 seconds on the ''iHP48'' emulator running on a iPhone Xr; first 20 strong Achilles numbers found in 5 minutes 13 seconds on the same platform.
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
def achilles?(n)
exponents = n.prime_division.map(&:last)
exponents.none?(1) && exponents.inject(&:gcd) == 1
end
 
def 𝜑(n)
n.prime_division.inject(1){|res, (pr, exp)| res * (pr-1) * pr**(exp-1) }
end
 
achilleses = (2..).lazy.select{|n| achilles?(n) }
 
n = 50
puts "First #{n} Achilles numbers:"
achilleses.first(n).each_slice(10){|s| puts "%9d"*s.size % s}
 
puts "\nFirst #{n} strong Achilles numbers:"
achilleses.select{|ach| achilles?(𝜑(ach)) }.first(n).each_slice(10){|s| puts "%9d"*s.size % s }
 
puts
counts = achilleses.take_while{|ach| ach < 1000000}.map{|a| a.digits.size }.tally
counts.each{|k, v| puts "#{k} digits: #{v}" }
</syntaxhighlight>
{{out}}
<pre>First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 50 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
81000 83349 84375 93312 108000 111132 124416 128000 135000 148176
151875 158184 162000 165888 172872 177957 197568 200000 202612 209952
 
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
6 digits: 664
</pre>
 
=={{header|Rust}}==
{{trans|Wren}}
<langsyntaxhighlight lang="rust">fn perfect_powers(n: u128) -> Vec<u128> {
let mut powers = Vec::<u128>::new();
let sqrt = (n as f64).sqrt() as u128;
Line 2,583 ⟶ 3,520:
let duration = t0.elapsed();
println!("\nElapsed time: {} milliseconds", duration.as_millis());
}</langsyntaxhighlight>
 
{{out}}
Line 2,617 ⟶ 3,554:
 
Elapsed time: 12608 milliseconds
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var P = 2.powerful(1e9)
var achilles = Set(P.grep{ !.is_power }...)
var strong_achilles = achilles.grep { achilles.has(.phi) }
 
say "First 50 Achilles numbers:"
achilles.sort.first(50).slices(10).each { .map{'%4s'%_}.join(' ').say }
 
say "\nFirst 30 strong Achilles numbers:"
strong_achilles.sort.first(30).slices(10).each { .map{'%5s'%_}.join(' ').say }
 
say "\nNumber of Achilles numbers with:"
achilles.to_a.group_by{.len}.sort_by{|k| k.to_i }.each_2d{|a,b|
say "#{a} digits: #{b.len}"
}</syntaxhighlight>
 
{{out}}
<pre>
First 50 Achilles numbers:
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
 
First 30 strong Achilles numbers:
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
 
Number of Achilles numbers with:
2 digits: 1
3 digits: 12
4 digits: 47
5 digits: 192
6 digits: 664
7 digits: 2242
8 digits: 7395
9 digits: 24008
</pre>
 
Line 2,625 ⟶ 3,603:
===Version 1 (Brute force)===
This finds the number of 6 digit Achilles numbers in 2.5 seconds, 7 digits in 51 seconds but 8 digits needs a whopping 21 minutes!
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
Line 2,632 ⟶ 3,610:
var limit = 10.pow(maxDigits)
var c = Int.primeSieve(limit-1, false)
 
var totient = Fn.new { |n|
var tot = n
var i = 2
while (i*i <= n) {
if (n%i == 0) {
while(n%i == 0) n = (n/i).floor
tot = tot - (tot/i).floor
}
if (i == 2) i = 1
i = i + 2
}
if (n > 1) tot = tot - (tot/n).floor
return tot
}
 
var isPerfectPower = Fn.new { |n|
Line 2,690 ⟶ 3,653:
var isStrongAchilles = Fn.new { |n|
if (!isAchilles.call(n)) return false
var tot = totientInt.calltotient(n)
return isAchilles.call(tot)
}
Line 2,705 ⟶ 3,668:
n = n + 1
}
Fmt.tprint("$4d", achilles, 10)
for (chunk in Lst.chunks(achilles, 10)) Fmt.print("$4d", chunk)
 
System.print("\nFirst 30 strong Achilles numbers:")
Line 2,718 ⟶ 3,681:
n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)
for (chunk in Lst.chunks(strongAchilles, 10)) Fmt.print("$5d", chunk)
 
System.print("\nNumber of Achilles numbers with:")
Line 2,729 ⟶ 3,692:
System.print("%(i) digits: %(count)")
pow = pow * 10
}</langsyntaxhighlight>
 
{{out}}
Line 2,759 ⟶ 3,722:
 
Ridiculously fast compared to the previous method: 12 digits can now be reached in 1.03 seconds, 13 digits in 3.7 seconds, 14 digits in 12.2 seconds and 15 digits in 69 seconds.
<langsyntaxhighlight ecmascriptlang="wren">import "./set" for Set
import "./seq" for Lst
import "./math" for Int
import "./fmt" for Fmt
 
var totient = Fn.new { |n|
var tot = n
var i = 2
while (i*i <= n) {
if (n%i == 0) {
while(n%i == 0) n = (n/i).floor
tot = tot - (tot/i).floor
}
if (i == 2) i = 1
i = i + 2
}
if (n > 1) tot = tot - (tot/n).floor
return tot
}
 
var pps = Set.new()
Line 2,813 ⟶ 3,762:
 
System.print("First 50 Achilles numbers:")
Fmt.tprint("$4d", achilles.take(50), 10)
for (chunk in Lst.chunks(achilles[0..49], 10)) Fmt.print("$4d", chunk)
 
System.print("\nFirst 30 strong Achilles numbers:")
Line 2,820 ⟶ 3,769:
var n = 0
while (count < 30) {
var tot = totientInt.calltotient(achilles[n])
if (achillesSet.contains(tot)) {
strongAchilles.add(achilles[n])
Line 2,827 ⟶ 3,776:
n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)
for (chunk in Lst.chunks(strongAchilles, 10)) Fmt.print("$5d", chunk)
 
System.print("\nNumber of Achilles numbers with:")
Line 2,833 ⟶ 3,782:
var ac = getAchilles.call(d-1, d).count
Fmt.print("$2d digits: $d", d, ac)
}</langsyntaxhighlight>
 
{{out}}
Line 2,867 ⟶ 3,816:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func GCD(N, D); \Return the greatest common divisor of N and D
int N, D; \numerator and denominator
int R;
Line 2,956 ⟶ 3,905:
IntOut(0, Cnt); CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
2,442

edits