Home primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 47:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting kernel make math math.parser math.primes
math.primes.factors math.ranges present prettyprint sequences
sequences.extras ;
Line 67:
: chain. ( n -- ) dup prime? [ prime. ] [ multi. ] if ;
 
2 20 [a,b] [ chain. ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 94:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 239:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 266:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">step =: -.&' '&.":@q:
hseq =: [,$:@step`(0&$)@.(1&p:)
fmtHP =: (' is prime',~":@])`('HP',":@],'(',":@[,')'&[)@.(*@[)
Line 272:
printHP =: 0 0&$@stdout@(fmtlist@hseq,(10{a.)&[)
printHP"0 [ 2}.i.21
exit 0</langsyntaxhighlight>
{{out}}
<pre>2 is prime
Line 295:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function homeprimechain(n::BigInt)
Line 316:
printHPiter(i)
end
</langsyntaxhighlight>{{out}}
<pre>
Home Prime chain for 2: 2 is prime.
Line 374:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[HP, HPChain]
HP[n_] := FromDigits[Catenate[IntegerDigits /@ Sort[Catenate[ConstantArray @@@ FactorInteger[n]]]]]
HPChain[n_] := NestWhileList[HP, n, PrimeQ/*Not]
Row[Prepend["Home prime chain for " <> ToString[#] <> ": "]@Riffle[HPChain[#], ", "]] & /@ Range[2, 20] // Column
Row[Prepend["Home prime chain for 65: "]@Riffle[HPChain[65], ", "]]</langsyntaxhighlight>
{{out}}
<pre>Home prime chain for 2: 2
Line 406:
{{libheader|bignum}}
This algorithm is really efficient. We get the result for HP2 to HP20 in about 6 ms and adding HP65 in 1.3 s. I think that the threshold to switch to Pollard-Rho is very important.
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat, strutils
import bignum
 
Line 498:
break
else:
inc n</langsyntaxhighlight>
 
{{out}}
Line 524:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'factor';
Line 535:
else { print "HP$m = " }
print "$steps[-1]\n";
}</langsyntaxhighlight>
 
{{out}}
Line 561:
=={{header|Phix}}==
Added a new mpz_pollard_rho routine, based on the [[wp:Pollard%27s_rho_algorithm]] C code.
<!--<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.0"</span><span style="color: #0000FF;">)</span>
Line 597:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">65</span><span style="color: #0000FF;">,</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
Using underscores to show the individual factors that were concatenated together
Line 690:
Using [https://modules.raku.org/search/?q=Prime+Factor Prime::Factor] from the [https://modules.raku.org/ Raku ecosystem].
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
my $start = now;
Line 713:
$now = now;
last if $step > 30;
}</langsyntaxhighlight>
{{out}}
<pre>HP2 = 2 (0.000 seconds)
Line 771:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the home prime of a range of positive integers. */
numeric digits 20 /*ensure handling of larger integers. */
parse arg LO HI . /*obtain optional arguments from the CL*/
Line 816:
/* [↓] The $ list has a leading blank.*/
if x==1 then return $ /*Is residual=unity? Then don't append.*/
return $ x /*return $ with appended residual. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 841:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">for n in (2..20, 65) {
 
var steps = []
Line 853:
 
say ("HP(#{orig}) = ", steps.map { .join('_') }.join(' -> '))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 886:
 
Reaches HP20 in about 0.52 seconds but HP65 took just under 40 minutes!
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/big" for BigInt
Line 911:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 941:
{{libheader|Wren-gmp}}
This reduces the overall time taken to 5.1 seconds. The factorization method used is essentially the same as the Wren-cli version so the vast improvement in performance is due solely to the use of GMP.
<langsyntaxhighlight lang="ecmascript">/* home_primes_gmp.wren */
 
import "./gmp" for Mpz
Line 968:
}
}
}</langsyntaxhighlight>
 
{{out}}
10,333

edits