Ruth-Aaron numbers: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 37: | Line 37: | ||
This uses a large amount of memory - too much for Algol 68G under Windows (and possibly under Linux).<br> |
This uses a large amount of memory - too much for Algol 68G under Windows (and possibly under Linux).<br> |
||
With max number set to 1 000 000, Algol 68G can find the first triple using factors in a few seconds (the loop to find the first divisors triple must be commented out or removed) - Real time: 0.941 s on TIO.RUN for the cutdown version. |
With max number set to 1 000 000, Algol 68G can find the first triple using factors in a few seconds (the loop to find the first divisors triple must be commented out or removed) - Real time: 0.941 s on TIO.RUN for the cutdown version. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find Ruth-Aaron pairs - pairs of consecutive integers where the sum # |
||
# of the prime factors or divisors are equal # |
# of the prime factors or divisors are equal # |
||
INT max number = 99 000 000; # max number we will consider # |
INT max number = 99 000 000; # max number we will consider # |
||
Line 139: | Line 139: | ||
print( ( newline, "First Ruth-Aaron triple (factors): ", whole( fra3, 0 ) ) ); |
print( ( newline, "First Ruth-Aaron triple (factors): ", whole( fra3, 0 ) ) ); |
||
print( ( newline, "First Ruth-Aaron triple (divisors): ", whole( dra3, 0 ) ) ) |
print( ( newline, "First Ruth-Aaron triple (divisors): ", whole( dra3, 0 ) ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 157: | Line 157: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
This takes about 2 minutes 24 seconds (3.2GHz Quad-Core Intel Core i5). |
This takes about 2 minutes 24 seconds (3.2GHz Quad-Core Intel Core i5). |
||
< |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 243: | Line 243: | ||
dsum2 = dsum3; |
dsum2 = dsum3; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 264: | Line 264: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2022-04-03}} |
{{works with|Factor|0.99 2022-04-03}} |
||
< |
<syntaxhighlight lang="factor">USING: assocs.extras grouping io kernel lists lists.lazy math |
||
math.primes.factors prettyprint ranges sequences ; |
math.primes.factors prettyprint ranges sequences ; |
||
Line 283: | Line 283: | ||
"First 30 Ruth-Aaron numbers (divisors):" print |
"First 30 Ruth-Aaron numbers (divisors):" print |
||
RA-d list.</ |
RA-d list.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 301: | Line 301: | ||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
Takes about 4.5 minutes. |
Takes about 4.5 minutes. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 382: | Line 382: | ||
fmt.Println("\nFirst Ruth-Aaron triple (divisors):") |
fmt.Println("\nFirst Ruth-Aaron triple (divisors):") |
||
fmt.Println(resT[0]) |
fmt.Println(resT[0]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 400: | Line 400: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Set as S |
||
import Data.List.Split ( chunksOf ) |
import Data.List.Split ( chunksOf ) |
||
Line 450: | Line 450: | ||
putStrLn "First 30 Ruth-Aaron numbers( divisors ):" |
putStrLn "First 30 Ruth-Aaron numbers( divisors ):" |
||
mapM_ (\nlin -> putStrLn $ foldl1 ( ++ ) $ map (\st -> formatNumber (maxlen2 + 2) st ) |
mapM_ (\nlin -> putStrLn $ foldl1 ( ++ ) $ map (\st -> formatNumber (maxlen2 + 2) st ) |
||
nlin ) numberlines2</ |
nlin ) numberlines2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 30 Ruth-Aaaron numbers ( factors ) : |
<pre>First 30 Ruth-Aaaron numbers ( factors ) : |
||
Line 470: | Line 470: | ||
Thus: |
Thus: |
||
< |
<syntaxhighlight lang="j"> NB. using factors |
||
30{.1 2+/~I. 2 =/\ +/@q: 1+i.100000 |
30{.1 2+/~I. 2 =/\ +/@q: 1+i.100000 |
||
5 6 |
5 6 |
||
Line 534: | Line 534: | ||
26642 26643 |
26642 26643 |
||
35456 35457 |
35456 35457 |
||
40081 40082</ |
40081 40082</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Lazy |
||
using Primes |
using Primes |
||
Line 563: | Line 563: | ||
println("\nRuth Aaron triple starts at: ", findfirst(ruthaarontriple, 1:100000000)) |
println("\nRuth Aaron triple starts at: ", findfirst(ruthaarontriple, 1:100000000)) |
||
println("\nRuth Aaron factor triple starts at: ", findfirst(ruthaaronfactorstriple, 1:10000000)) |
println("\nRuth Aaron factor triple starts at: ", findfirst(ruthaaronfactorstriple, 1:10000000)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
30 Ruth Aaron numbers: |
30 Ruth Aaron numbers: |
||
Line 583: | Line 583: | ||
==={{header|Free Pascal}}=== |
==={{header|Free Pascal}}=== |
||
all depends on fast prime decomposition. |
all depends on fast prime decomposition. |
||
< |
<syntaxhighlight lang="pascal">program RuthAaronNumb; |
||
// gets factors of consecutive integers fast |
// gets factors of consecutive integers fast |
||
// limited to 1.2e11 |
// limited to 1.2e11 |
||
Line 989: | Line 989: | ||
writeln('First Ruth-Aaron triple (divisors):'); |
writeln('First Ruth-Aaron triple (divisors):'); |
||
writeln(findfirstTripplesFactor(false):10); |
writeln(findfirstTripplesFactor(false):10); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out|@TIO.RUN}} |
{{out|@TIO.RUN}} |
||
<pre> |
<pre> |
||
Line 1,012: | Line 1,012: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 1,037: | Line 1,037: | ||
$n++; |
$n++; |
||
} |
} |
||
print "divisors:\n\n@answers\n" =~ s/.{60}\K /\n/gr;</ |
print "divisors:\n\n@answers\n" =~ s/.{60}\K /\n/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,056: | Line 1,056: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/ruthaaron.htm here]. |
You can run this online [http://phix.x10.mx/p2js/ruthaaron.htm here]. |
||
<!--< |
<!--<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: #008080;">procedure</span> <span style="color: #000000;">ruth_aaron</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">ruth_aaron</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
||
Line 1,090: | Line 1,090: | ||
<span style="color: #000000;">ruth_aaron</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">89460000</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (0.1s) |
<span style="color: #000000;">ruth_aaron</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">89460000</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (0.1s) |
||
--ruth_aaron(true, 1, 3) -- (24 minutes 30s)</span> |
--ruth_aaron(true, 1, 3) -- (24 minutes 30s)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,110: | Line 1,110: | ||
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]]. |
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]]. |
||
< |
<syntaxhighlight lang="quackery"> [ behead dup dip nested rot |
||
witheach |
witheach |
||
[ tuck != if |
[ tuck != if |
||
Line 1,149: | Line 1,149: | ||
raf echo |
raf echo |
||
cr cr |
cr cr |
||
rad echo</ |
rad echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,160: | Line 1,160: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>use Prime::Factor; |
||
my @pf = lazy (^∞).hyper(:1000batch).map: *.&prime-factors.sum; |
my @pf = lazy (^∞).hyper(:1000batch).map: *.&prime-factors.sum; |
||
Line 1,178: | Line 1,178: | ||
# Really, really, _really_ slow. 186(!) minutes... but with no cheating or "leg up". |
# Really, really, _really_ slow. 186(!) minutes... but with no cheating or "leg up". |
||
put "\nFirst Ruth-Aaron triple (Divisors):\n" ~ |
put "\nFirst Ruth-Aaron triple (Divisors):\n" ~ |
||
(1..∞).first: { @upf[$_] == @upf[$_ + 1] == @upf[$_ + 2] }</ |
(1..∞).first: { @upf[$_] == @upf[$_ + 1] == @upf[$_ + 2] }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 30 Ruth-Aaron numbers (Factors): |
<pre>First 30 Ruth-Aaron numbers (Factors): |
||
Line 1,193: | Line 1,193: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">say "First 30 Ruth-Aaron numbers (factors):" |
||
say 30.by {|n| (sopfr(n) == sopfr(n+1)) && (n > 0) }.join(' ') |
say 30.by {|n| (sopfr(n) == sopfr(n+1)) && (n > 0) }.join(' ') |
||
say "\nFirst 30 Ruth-Aaron numbers (divisors):" |
say "\nFirst 30 Ruth-Aaron numbers (divisors):" |
||
say 30.by {|n| ( sopf(n) == sopf(n+1)) && (n > 0) }.join(' ')</ |
say 30.by {|n| ( sopf(n) == sopf(n+1)) && (n > 0) }.join(' ')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,215: | Line 1,215: | ||
However, with nearly 90 million trios of numbers to slog through, it takes around 68 minutes to find the first triple based on divisors. |
However, with nearly 90 million trios of numbers to slog through, it takes around 68 minutes to find the first triple based on divisors. |
||
< |
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums |
||
import "./seq" for Lst |
import "./seq" for Lst |
||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
Line 1,292: | Line 1,292: | ||
System.print("\nFirst Ruth-Aaron triple (divisors):") |
System.print("\nFirst Ruth-Aaron triple (divisors):") |
||
System.print(resT[0])</ |
System.print(resT[0])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,310: | Line 1,310: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func DivSum(N, AllDiv); \Return sum of divisors |
||
int N, AllDiv; \all divisors vs. only prime divisors |
int N, AllDiv; \all divisors vs. only prime divisors |
||
int F, F0, S, Q; |
int F, F0, S, Q; |
||
Line 1,345: | Line 1,345: | ||
CrLf(0); |
CrLf(0); |
||
Ruth(false); |
Ruth(false); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |