Ruth-Aaron numbers: Difference between revisions

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

edits