Ruth-Aaron numbers: Difference between revisions

Content added Content deleted
No edit summary
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.
<lang algol68>BEGIN # find Ruth-Aaron pairs - pairs of consecutive integers where the sum #
<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</lang>
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).
<lang cpp>#include <iomanip>
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <iostream>


Line 243: Line 243:
dsum2 = dsum3;
dsum2 = dsum3;
}
}
}</lang>
}</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}}
<lang factor>USING: assocs.extras grouping io kernel lists lists.lazy math
<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.</lang>
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.
<lang go>package main
<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])
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 400: Line 400:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import qualified Data.Set as S
<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</lang>
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:
<lang J> NB. using factors
<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</lang>
40081 40082</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Lazy
<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))
</lang>{{out}}
</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.
<lang pascal>program RuthAaronNumb;
<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.</lang>
end.</syntaxhighlight>
{{out|@TIO.RUN}}
{{out|@TIO.RUN}}
<pre>
<pre>
Line 1,012: Line 1,012:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/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;</lang>
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].
<!--<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: #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>
<!--</lang>-->
<!--</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]].


<lang Quackery> [ behead dup dip nested rot
<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</lang>
rad echo</syntaxhighlight>


{{out}}
{{out}}
Line 1,160: Line 1,160:
=={{header|Raku}}==
=={{header|Raku}}==


<lang perl6>use Prime::Factor;
<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] }</lang>
(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}}==
<lang ruby>say "First 30 Ruth-Aaron numbers (factors):"
<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(' ')</lang>
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.
<lang ecmascript>import "./math" for Int, Nums
<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])</lang>
System.print(resT[0])</syntaxhighlight>


{{out}}
{{out}}
Line 1,310: Line 1,310:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func DivSum(N, AllDiv); \Return sum of divisors
<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);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}