Achilles numbers: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 48: Line 48:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<lang AArch64 Assembly>
<syntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program achilleNumber.s */
/* program achilleNumber.s */
Line 764: Line 764:
/***************************************************/
/***************************************************/
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
First 50 Achilles Numbers:
First 50 Achilles Numbers:
Line 785: Line 785:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find Achilles Numbers: numbers whose prime factors p appear at least #
<syntaxhighlight 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 #
# 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 #
# expressed as m^k for any integer m, k > 1 #
Line 901: Line 901:
OD
OD
END
END
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Achilles Numbers:
<pre>First 50 Achilles Numbers:
Line 919: Line 919:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<lang ARM Assembly>
<syntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program achilleNumber.s */
/* program achilleNumber.s */
Line 1,546: Line 1,546:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
First 50 Achilles Numbers:
First 50 Achilles Numbers:
Line 1,566: Line 1,566:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Boost}}
{{libheader|Boost}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang=cpp>#include <algorithm>
#include <chrono>
#include <chrono>
#include <cmath>
#include <cmath>
Line 1,658: Line 1,658:
std::chrono::duration<double> duration(end - start);
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,697: Line 1,697:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>Function GCD(n As Uinteger, d As Uinteger) As Uinteger
<syntaxhighlight lang=freebasic>Function GCD(n As Uinteger, d As Uinteger) As Uinteger
Return Iif(d = 0, n, GCD(d, n Mod d))
Return Iif(d = 0, n, GCD(d, n Mod d))
End Function
End Function
Line 1,784: Line 1,784:
Print i; " digits:"; num
Print i; " digits:"; num
Next i
Next i
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Achilles numbers:
<pre>First 50 Achilles numbers:
Line 1,808: Line 1,808:
{{trans|Wren}}
{{trans|Wren}}
Based on Version 2, takes around 19 seconds.
Based on Version 2, takes around 19 seconds.
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 1,917: Line 1,917:
fmt.Printf("%2d digits: %d\n", d, ac)
fmt.Printf("%2d digits: %d\n", d, ac)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,953: Line 1,953:
Implementation:
Implementation:


<lang J>achilles=: (*/ .>&1 * 1 = +./)@(1{__&q:)"0
<syntaxhighlight lang=J>achilles=: (*/ .>&1 * 1 = +./)@(1{__&q:)"0
strong=: achilles@(5&p:)</lang>
strong=: achilles@(5&p:)</syntaxhighlight>


Task examples:
Task examples:


<lang J> 5 10$(#~ achilles) 1+i.10000 NB. first 50 achilles numbers
<syntaxhighlight lang=J> 5 10$(#~ achilles) 1+i.10000 NB. first 50 achilles numbers
72 108 200 288 392 432 500 648 675 800
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
864 968 972 1125 1152 1323 1352 1372 1568 1800
Line 1,977: Line 1,977:
192
192
+/achilles (+i.)/1 9*10^<:6
+/achilles (+i.)/1 9*10^<:6
664</lang>
664</syntaxhighlight>


Explanation of the code:
Explanation of the code:
Line 2,030: Line 2,030:


teststrongachilles()
teststrongachilles()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 50 Achilles numbers:
First 50 Achilles numbers:
Line 2,060: Line 2,060:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[PowerfulNumberQ, StrongAchillesNumberQ]
<syntaxhighlight lang=Mathematica>ClearAll[PowerfulNumberQ, StrongAchillesNumberQ]
PowerfulNumberQ[n_Integer] := AllTrue[FactorInteger[n][[All, 2]], GreaterEqualThan[2]]
PowerfulNumberQ[n_Integer] := AllTrue[FactorInteger[n][[All, 2]], GreaterEqualThan[2]]
AchillesNumberQ[n_Integer] := Module[{divs},
AchillesNumberQ[n_Integer] := Module[{divs},
Line 2,091: Line 2,091:
]][[2, 1]]
]][[2, 1]]


Tally[IntegerLength /@ Select[Range[9999999], AchillesNumberQ]] // Grid</lang>
Tally[IntegerLength /@ Select[Range[9999999], AchillesNumberQ]] // Grid</syntaxhighlight>
{{out}}
{{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}
<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,107: Line 2,107:
Borrowed, and lightly modified, code from [[Powerful_numbers]]
Borrowed, and lightly modified, code from [[Powerful_numbers]]
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use feature <say current_sub>;
use feature <say current_sub>;
Line 2,140: Line 2,140:
my $c; $l == length and $c++ for @achilles;
my $c; $l == length and $c++ for @achilles;
say "$l digits: $c";
say "$l digits: $c";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Achilles numbers:
<pre>First 50 Achilles numbers:
Line 2,164: Line 2,164:
9 digits: 24008</pre>
9 digits: 24008</pre>
Here is a translation from Wren version 2, as an alternative.
Here is a translation from Wren version 2, as an alternative.
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;


Line 2,232: Line 2,232:
for my $d (2..$maxDigits) {
for my $d (2..$maxDigits) {
printf "%2d digits: %d\n", $d, scalar getAchilles($d-1, $d)
printf "%2d digits: %d\n", $d, scalar getAchilles($d-1, $d)
}</lang>
}</syntaxhighlight>
Output is the same.
Output is the same.


Line 2,239: Line 2,239:
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.
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}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 2,300:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,330: Line 2,330:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from math import gcd
<syntaxhighlight lang=python>from math import gcd
from sympy import factorint
from sympy import factorint
Line 2,367: Line 2,367:


test_strong_Achilles(50, 100)
test_strong_Achilles(50, 100)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 50 Achilles numbers:
First 50 Achilles numbers:
Line 2,398: Line 2,398:
=={{header|Raku}}==
=={{header|Raku}}==
Timing is going to be system / OS dependent.
Timing is going to be system / OS dependent.
<lang perl6>use Prime::Factor;
<syntaxhighlight lang=perl6>use Prime::Factor;
use Math::Root;
use Math::Root;


Line 2,443: Line 2,443:
say "$_ digits: " ~ +$achilles{$_} // 0 for 2..9;
say "$_ digits: " ~ +$achilles{$_} // 0 for 2..9;


printf "\n%.1f total elapsed seconds\n", now - INIT now;</lang>
printf "\n%.1f total elapsed seconds\n", now - INIT now;</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Achilles numbers:
<pre>First 50 Achilles numbers:
Line 2,479: Line 2,479:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|Wren}}
{{trans|Wren}}
<lang rust>fn perfect_powers(n: u128) -> Vec<u128> {
<syntaxhighlight lang=rust>fn perfect_powers(n: u128) -> Vec<u128> {
let mut powers = Vec::<u128>::new();
let mut powers = Vec::<u128>::new();
let sqrt = (n as f64).sqrt() as u128;
let sqrt = (n as f64).sqrt() as u128;
Line 2,583: Line 2,583:
let duration = t0.elapsed();
let duration = t0.elapsed();
println!("\nElapsed time: {} milliseconds", duration.as_millis());
println!("\nElapsed time: {} milliseconds", duration.as_millis());
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,625: Line 2,625:
===Version 1 (Brute force)===
===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!
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!
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang=ecmascript>import "./math" for Int
import "./seq" for Lst
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 2,729: Line 2,729:
System.print("%(i) digits: %(count)")
System.print("%(i) digits: %(count)")
pow = pow * 10
pow = pow * 10
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,759: Line 2,759:


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.
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.
<lang ecmascript>import "./set" for Set
<syntaxhighlight lang=ecmascript>import "./set" for Set
import "./seq" for Lst
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 2,833: Line 2,833:
var ac = getAchilles.call(d-1, d).count
var ac = getAchilles.call(d-1, d).count
Fmt.print("$2d digits: $d", d, ac)
Fmt.print("$2d digits: $d", d, ac)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,867: Line 2,867:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func GCD(N, D); \Return the greatest common divisor of N and D
<syntaxhighlight lang=XPL0>func GCD(N, D); \Return the greatest common divisor of N and D
int N, D; \numerator and denominator
int N, D; \numerator and denominator
int R;
int R;
Line 2,956: Line 2,956:
IntOut(0, Cnt); CrLf(0);
IntOut(0, Cnt); CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}