Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Home-primes/00-META.yaml
Normal file
3
Task/Home-primes/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Home_primes
|
||||
note: Prime Numbers
|
||||
43
Task/Home-primes/00-TASK.txt
Normal file
43
Task/Home-primes/00-TASK.txt
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
In number theory, the '''home prime HP(n)''' of an integer '''n''' greater than 1 is the prime number obtained by repeatedly factoring the increasing concatenation of prime factors including repetitions.
|
||||
|
||||
The traditional notation has the prefix "HP" and a postfix count of the number of iterations until the home prime is found (if the count is greater than 0), for instance HP4(2) === HP22(1) === 211 is the same as saying the home prime of 4 needs 2 iterations and is the same as the home prime of 22 which needs 1 iteration, and (both) resolve to 211, a prime.
|
||||
|
||||
Prime numbers are their own home prime;
|
||||
|
||||
So:
|
||||
|
||||
HP2 = 2
|
||||
|
||||
HP7 = 7
|
||||
|
||||
If the integer obtained by concatenating increasing prime factors is not prime, iterate until you reach a prime number; the home prime.
|
||||
|
||||
HP4(2) = HP22(1) = 211
|
||||
HP4(2) = 2 × 2 => 22; HP22(1) = 2 × 11 => 211; 211 is prime
|
||||
|
||||
HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773
|
||||
HP10(4) = 2 × 5 => 25; HP25(3) = 5 × 5 => 55; HP55(2) = 5 × 11 => 511; HP511(1) = 7 × 73 => 773; 773 is prime
|
||||
|
||||
|
||||
;Task
|
||||
|
||||
* Find and show here, on this page, the home prime iteration chains for the integers 2 through 20 inclusive.
|
||||
|
||||
|
||||
;Stretch goal
|
||||
|
||||
* Find and show the iteration chain for 65.
|
||||
|
||||
|
||||
;Impossible goal
|
||||
|
||||
* Show the the home prime for HP49.
|
||||
|
||||
|
||||
;See also
|
||||
|
||||
;* [[oeis:A037274|OEIS:A037274 - Home primes for n >= 2]]
|
||||
;* [[oeis:A056938|OEIS:A056938 - Concatenation chain for HP49]]
|
||||
|
||||
<br/>
|
||||
|
||||
21
Task/Home-primes/Factor/home-primes.factor
Normal file
21
Task/Home-primes/Factor/home-primes.factor
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
USING: formatting kernel make math math.parser math.primes
|
||||
math.primes.factors math.ranges present prettyprint sequences
|
||||
sequences.extras ;
|
||||
|
||||
: squish ( seq -- n ) [ present ] map-concat dec> ;
|
||||
|
||||
: next ( m -- n ) factors squish ; inline
|
||||
|
||||
: (chain) ( n -- ) [ dup prime? ] [ dup , next ] until , ;
|
||||
|
||||
: chain ( n -- seq ) [ (chain) ] { } make ;
|
||||
|
||||
: prime. ( n -- ) dup "HP%d = %d\n" printf ;
|
||||
|
||||
: setup ( seq -- n s r ) unclip-last swap dup length 1 [a,b] ;
|
||||
|
||||
: multi. ( n -- ) chain setup [ "HP%d(%d) = " printf ] 2each . ;
|
||||
|
||||
: chain. ( n -- ) dup prime? [ prime. ] [ multi. ] if ;
|
||||
|
||||
2 20 [a,b] [ chain. ] each
|
||||
146
Task/Home-primes/Go/home-primes.go
Normal file
146
Task/Home-primes/Go/home-primes.go
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"rcu"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var zero = new(big.Int)
|
||||
var one = big.NewInt(1)
|
||||
var two = big.NewInt(2)
|
||||
var three = big.NewInt(3)
|
||||
var four = big.NewInt(4)
|
||||
var five = big.NewInt(5)
|
||||
var six = big.NewInt(6)
|
||||
|
||||
// simple wheel based prime factors routine for BigInt
|
||||
func primeFactorsWheel(m *big.Int) []*big.Int {
|
||||
n := new(big.Int).Set(m)
|
||||
t := new(big.Int)
|
||||
inc := []*big.Int{four, two, four, two, four, six, two, six}
|
||||
var factors []*big.Int
|
||||
for t.Rem(n, two).Cmp(zero) == 0 {
|
||||
factors = append(factors, two)
|
||||
n.Quo(n, two)
|
||||
}
|
||||
for t.Rem(n, three).Cmp(zero) == 0 {
|
||||
factors = append(factors, three)
|
||||
n.Quo(n, three)
|
||||
}
|
||||
for t.Rem(n, five).Cmp(zero) == 0 {
|
||||
factors = append(factors, five)
|
||||
n.Quo(n, five)
|
||||
}
|
||||
k := big.NewInt(7)
|
||||
i := 0
|
||||
for t.Mul(k, k).Cmp(n) <= 0 {
|
||||
if t.Rem(n, k).Cmp(zero) == 0 {
|
||||
factors = append(factors, new(big.Int).Set(k))
|
||||
n.Quo(n, k)
|
||||
} else {
|
||||
k.Add(k, inc[i])
|
||||
i = (i + 1) % 8
|
||||
}
|
||||
}
|
||||
if n.Cmp(one) > 0 {
|
||||
factors = append(factors, n)
|
||||
}
|
||||
return factors
|
||||
}
|
||||
|
||||
func pollardRho(n *big.Int) *big.Int {
|
||||
g := func(x, n *big.Int) *big.Int {
|
||||
x2 := new(big.Int)
|
||||
x2.Mul(x, x)
|
||||
x2.Add(x2, one)
|
||||
return x2.Mod(x2, n)
|
||||
}
|
||||
x, y, d := new(big.Int).Set(two), new(big.Int).Set(two), new(big.Int).Set(one)
|
||||
t, z := new(big.Int), new(big.Int).Set(one)
|
||||
count := 0
|
||||
for {
|
||||
x = g(x, n)
|
||||
y = g(g(y, n), n)
|
||||
t.Sub(x, y)
|
||||
t.Abs(t)
|
||||
t.Mod(t, n)
|
||||
z.Mul(z, t)
|
||||
count++
|
||||
if count == 100 {
|
||||
d.GCD(nil, nil, z, n)
|
||||
if d.Cmp(one) != 0 {
|
||||
break
|
||||
}
|
||||
z.Set(one)
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
if d.Cmp(n) == 0 {
|
||||
return new(big.Int)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func primeFactors(m *big.Int) []*big.Int {
|
||||
n := new(big.Int).Set(m)
|
||||
var factors []*big.Int
|
||||
lim := big.NewInt(1e9)
|
||||
for n.Cmp(one) > 0 {
|
||||
if n.Cmp(lim) > 0 {
|
||||
d := pollardRho(n)
|
||||
if d.Cmp(zero) != 0 {
|
||||
factors = append(factors, primeFactorsWheel(d)...)
|
||||
n.Quo(n, d)
|
||||
if n.ProbablyPrime(10) {
|
||||
factors = append(factors, n)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
factors = append(factors, primeFactorsWheel(n)...)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
factors = append(factors, primeFactorsWheel(n)...)
|
||||
break
|
||||
}
|
||||
}
|
||||
sort.Slice(factors, func(i, j int) bool { return factors[i].Cmp(factors[j]) < 0 })
|
||||
return factors
|
||||
}
|
||||
|
||||
func main() {
|
||||
list := make([]int, 20)
|
||||
for i := 2; i <= 20; i++ {
|
||||
list[i-2] = i
|
||||
}
|
||||
list[19] = 65
|
||||
for _, i := range list {
|
||||
if rcu.IsPrime(i) {
|
||||
fmt.Printf("HP%d = %d\n", i, i)
|
||||
continue
|
||||
}
|
||||
n := 1
|
||||
j := big.NewInt(int64(i))
|
||||
h := []*big.Int{j}
|
||||
for {
|
||||
pf := primeFactors(j)
|
||||
k := ""
|
||||
for _, f := range pf {
|
||||
k += fmt.Sprintf("%d", f)
|
||||
}
|
||||
j, _ = new(big.Int).SetString(k, 10)
|
||||
h = append(h, j)
|
||||
if j.ProbablyPrime(10) {
|
||||
for l := n; l > 0; l-- {
|
||||
fmt.Printf("HP%d(%d) = ", h[n-l], l)
|
||||
}
|
||||
fmt.Println(h[n])
|
||||
break
|
||||
} else {
|
||||
n++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Task/Home-primes/J/home-primes.j
Normal file
7
Task/Home-primes/J/home-primes.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
step =: -.&' '&.":@q:
|
||||
hseq =: [,$:@step`(0&$)@.(1&p:)
|
||||
fmtHP =: (' is prime',~":@])`('HP',":@],'(',":@[,')'&[)@.(*@[)
|
||||
fmtlist =: [:;@}.[:,(<' = ')&,"0@(|.@i.@# fmtHP each [)
|
||||
printHP =: 0 0&$@stdout@(fmtlist@hseq,(10{a.)&[)
|
||||
printHP"0 [ 2}.i.21
|
||||
exit 0
|
||||
21
Task/Home-primes/Julia/home-primes.julia
Normal file
21
Task/Home-primes/Julia/home-primes.julia
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Primes
|
||||
|
||||
function homeprimechain(n::BigInt)
|
||||
isprime(n) && return [n]
|
||||
concat = prod(string(i)^j for (i, j) in factor(n).pe)
|
||||
return pushfirst!(homeprimechain(parse(BigInt, concat)), n)
|
||||
end
|
||||
homeprimechain(n::Integer) = homeprimechain(BigInt(n))
|
||||
|
||||
function printHPiter(n, numperline = 4)
|
||||
chain = homeprimechain(n)
|
||||
len = length(chain)
|
||||
for (i, ent) in enumerate(chain)
|
||||
print(i < len ? "HP$ent" * "($(len - i)) = " * (i % numperline == 0 ? "\n" : "") : "$ent is prime.\n\n")
|
||||
end
|
||||
end
|
||||
|
||||
for i in [2:20; 65]
|
||||
print("Home Prime chain for $i: ")
|
||||
printHPiter(i)
|
||||
end
|
||||
5
Task/Home-primes/Mathematica/home-primes.math
Normal file
5
Task/Home-primes/Mathematica/home-primes.math
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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], ", "]]
|
||||
93
Task/Home-primes/Nim/home-primes.nim
Normal file
93
Task/Home-primes/Nim/home-primes.nim
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import algorithm, sequtils, strformat, strutils
|
||||
import bignum
|
||||
|
||||
let
|
||||
Two = newInt(2)
|
||||
Three = newInt(3)
|
||||
Five = newInt(5)
|
||||
|
||||
|
||||
proc primeFactorsWheel(n: Int): seq[Int] =
|
||||
const Inc = [4, 2, 4, 2, 4, 6, 2, 6]
|
||||
var n = n
|
||||
while (n mod 2).isZero:
|
||||
result.add Two
|
||||
n = n div 2
|
||||
while (n mod 3).isZero:
|
||||
result.add Three
|
||||
n = n div 3
|
||||
while (n mod 5).isZero:
|
||||
result.add Five
|
||||
n = n div 5
|
||||
var k = 7
|
||||
var i = 0
|
||||
while k * k <= n:
|
||||
if (n mod k).isZero:
|
||||
result.add newInt(k)
|
||||
n = n div k
|
||||
else:
|
||||
inc k, Inc[i]
|
||||
i = (i + 1) and 7
|
||||
if n > 1: result.add n
|
||||
|
||||
|
||||
func pollardRho(n : Int): Int =
|
||||
|
||||
func g(x, y: Int): Int = (x * x + 1) mod y
|
||||
|
||||
var x, y = newInt(2)
|
||||
var z, d = newInt(1)
|
||||
var count = 0
|
||||
while true:
|
||||
x = g(x, n)
|
||||
y = g(g(y, n), n)
|
||||
d = abs(x - y) mod n
|
||||
z *= d
|
||||
inc count
|
||||
if count == 100:
|
||||
d = gcd(z, n)
|
||||
if d != 1: break
|
||||
z = newInt(1)
|
||||
count = 0
|
||||
if d == n: return newInt(0)
|
||||
result = d
|
||||
|
||||
|
||||
proc primeFactors(n: Int): seq[Int] =
|
||||
var n = n
|
||||
while n > 1:
|
||||
if n > 100_000_000:
|
||||
let d = pollardRho(n)
|
||||
if not d.isZero:
|
||||
result.add primeFactorsWheel(d)
|
||||
n = n div d
|
||||
if n.probablyPrime(25) != 0:
|
||||
result.add n
|
||||
break
|
||||
else:
|
||||
result.add primeFactorsWheel(n)
|
||||
break
|
||||
else:
|
||||
result.add primeFactorsWheel(n)
|
||||
break
|
||||
result.sort()
|
||||
|
||||
|
||||
let list = toSeq(2..20) & 65
|
||||
for i in list:
|
||||
if i in [2, 3, 5, 7, 11, 13, 17, 19]:
|
||||
echo &"HP{i} = {i}"
|
||||
continue
|
||||
var n = 1
|
||||
var j = newInt(i)
|
||||
var h = @[j]
|
||||
while true:
|
||||
j = newInt(primeFactors(j).join())
|
||||
h.add j
|
||||
if j.probablyPrime(25) != 0:
|
||||
for k in countdown(n, 1):
|
||||
stdout.write &"HP{h[n-k]}({k}) = "
|
||||
echo h[n]
|
||||
break
|
||||
else:
|
||||
inc n
|
||||
12
Task/Home-primes/Perl/home-primes.pl
Normal file
12
Task/Home-primes/Perl/home-primes.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use ntheory 'factor';
|
||||
|
||||
for my $m (2..20, 65) {
|
||||
my (@steps, @factors) = $m;
|
||||
push @steps, join '_', @factors while (@factors = factor $steps[-1] =~ s/_//gr) > 1;
|
||||
my $step = $#steps;
|
||||
if ($step >= 1) { print 'HP' . $_ . "($step) = " and --$step or last for @steps }
|
||||
else { print "HP$m = " }
|
||||
print "$steps[-1]\n";
|
||||
}
|
||||
37
Task/Home-primes/Phix/home-primes.phix
Normal file
37
Task/Home-primes/Phix/home-primes.phix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
(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>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</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: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">lastp</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"_"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_pollard_rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rr</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rr</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"_"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t</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: #004080;">integer</span> <span style="color: #000000;">niter</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">iter</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">niter</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"(%d)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">niter</span><span style="color: #0000FF;">):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">></span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">" ["</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"]"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"HP%d%s = "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iter</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">niter</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s%d %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">niter</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">niter</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%sHP%s(%d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">niter</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'='</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s%s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[$],</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<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>
|
||||
<!--
|
||||
46
Task/Home-primes/REXX/home-primes.rexx
Normal file
46
Task/Home-primes/REXX/home-primes.rexx
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/*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*/
|
||||
if LO=='' | LO=="," then LO= 2 /*Not specified? Then use the default.*/
|
||||
if HI=='' | HI=="," then HI= 20 /* " " " " " " */
|
||||
@hpc= 'home prime chain for ' /*a literal used in two SAY statements.*/
|
||||
w= length(HI) /*HI width, used for output alignment. */
|
||||
do j=max(2, LO) to HI /*find home primes for an integer range*/
|
||||
pf= factr(j); f= words(pf) /*get prime factors; number of factors.*/
|
||||
if f==1 then do; say @hpc j": " j ' is prime.'; iterate; end /*J is prime*/
|
||||
xxx.1= j /*save J in the first array element. */
|
||||
do n=2 until #==1 /*keep processing until we find a prime*/
|
||||
xxx.n= space(pf, 0) /*obtain factors of a concatenated p.f.*/
|
||||
pf= factr(xxx.n); #= words(pf) /*assign factors to PF; # of factors. */
|
||||
end /*n*/
|
||||
ee= n /*save EE as the final (last) prime. */
|
||||
n= n - 1; z= n /*adjust N (for DO loop); assign N to Z*/
|
||||
$= /*nullify the string of home primes. */
|
||||
do m=1 for n /*build a list ($) of " " */
|
||||
$= $ 'HP'xxx.m"("z') ─► ' /*concatenate to string of " " */
|
||||
z= z - 1 /*decrease the index counter by unity. */
|
||||
end /*m*/ /* [↑] the index counter is decreasing*/
|
||||
|
||||
say @hpc right(j, w)":" $ xxx.ee ' is prime.' /*show string of home primes.*/
|
||||
end /*n*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
factr: procedure; parse arg x 1 d,$ /*set X, D to argument 1; $ to null.*/
|
||||
if x==1 then return '' /*handle the special case of X = 1. */
|
||||
do while x//2==0; $= $ 2; x= x% 2; end /*append all the 2 factors of new X.*/
|
||||
do while x//3==0; $= $ 3; x= x% 3; end /* " " " 3 " " " " */
|
||||
do while x//5==0; $= $ 5; x= x% 5; end /* " " " 5 " " " " */
|
||||
do while x//7==0; $= $ 7; x= x% 7; end /* " " " 7 " " " " */
|
||||
q= 1; r= 0 /*R: will be iSqrt(x). ___*/
|
||||
do while q<=x; q=q*4; end /*these two lines compute integer √ X */
|
||||
do while q>1; q=q%4; _= d-r-q; r= r%2; if _>=0 then do; d= _; r= r+q; end; end
|
||||
|
||||
do k=11 by 6 to r /*insure that J isn't divisible by 3.*/
|
||||
parse var k '' -1 _ /*obtain the last decimal digit of K. */
|
||||
if _\==5 then do while x//k==0; $=$ k; x=x%k; end /*maybe reduce by K.*/
|
||||
if _ ==3 then iterate /*Is next Y is divisible by 5? Skip.*/
|
||||
y= k+2; do while x//y==0; $=$ y; x=x%y; end /*maybe reduce by Y.*/
|
||||
end /*k*/
|
||||
/* [↓] The $ list has a leading blank.*/
|
||||
if x==1 then return $ /*Is residual=unity? Then don't append.*/
|
||||
return $ x /*return $ with appended residual. */
|
||||
24
Task/Home-primes/Raku/home-primes.raku
Normal file
24
Task/Home-primes/Raku/home-primes.raku
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use Prime::Factor;
|
||||
|
||||
my $start = now;
|
||||
|
||||
(flat 2..20, 65).map: -> $m {
|
||||
my ($now, @steps, @factors) = now, $m;
|
||||
|
||||
@steps.push: @factors.join('_') while (@factors = prime-factors @steps[*-1].Int) > 1;
|
||||
|
||||
say (my $step = +@steps) > 1 ?? (@steps[0..*-2].map( { "HP$_\({--$step})" } ).join: ' = ') !! ("HP$m"),
|
||||
" = ", @steps[*-1], " ({(now - $now).fmt("%0.3f")} seconds)";
|
||||
}
|
||||
|
||||
say "Total elapsed time: {(now - $start).fmt("%0.3f")} seconds\n";
|
||||
|
||||
say 'HP49:';
|
||||
my ($now, @steps, @factors) = now, 49;
|
||||
my $step = 0;
|
||||
while (@factors = prime-factors @steps[*-1].Int) > 1 {
|
||||
@steps.push: @factors.join('_');
|
||||
say "HP{@steps[$step].Int}\(n - {$step++}) = ", @steps[*-1], " ({(now - $now).fmt("%0.3f")} seconds)";
|
||||
$now = now;
|
||||
last if $step > 30;
|
||||
}
|
||||
13
Task/Home-primes/Sidef/home-primes.sidef
Normal file
13
Task/Home-primes/Sidef/home-primes.sidef
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
for n in (2..20, 65) {
|
||||
|
||||
var steps = []
|
||||
var orig = n
|
||||
|
||||
for (var f = n.factor; true; f = n.factor) {
|
||||
steps << f
|
||||
n = Num(f.join)
|
||||
break if n.is_prime
|
||||
}
|
||||
|
||||
say ("HP(#{orig}) = ", steps.map { .join('_') }.join(' -> '))
|
||||
}
|
||||
23
Task/Home-primes/Transd/home-primes.transd
Normal file
23
Task/Home-primes/Transd/home-primes.transd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#lang transd
|
||||
|
||||
MainModule: {
|
||||
homePrime: (λ i BigLong()
|
||||
(if (is-prime i) (lout "HP" i " = " i) continue)
|
||||
(with n BigLong(i) ch Vector<BigLong>() st 1
|
||||
(append ch n)
|
||||
(while true
|
||||
(= n (join (prime-factors n) ""))
|
||||
(append ch n)
|
||||
(if (is-probable-prime n 15)
|
||||
(for l in Range(in: ch 0 -1) do
|
||||
(textout "HP" l "("(- (size ch) @idx 1) ") = "))
|
||||
(lout (back ch)) (ret)
|
||||
else (+= st 1))
|
||||
)
|
||||
)
|
||||
),
|
||||
_start: (λ
|
||||
(for i in Range(2 21) do (homePrime BigLong(i)))
|
||||
(homePrime BigLong(65))
|
||||
)
|
||||
}
|
||||
26
Task/Home-primes/Wren/home-primes-1.wren
Normal file
26
Task/Home-primes/Wren/home-primes-1.wren
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import "/math" for Int
|
||||
import "/big" for BigInt
|
||||
|
||||
var list = (2..20).toList
|
||||
list.add(65)
|
||||
for (i in list) {
|
||||
if (Int.isPrime(i)) {
|
||||
System.print("HP%(i) = %(i)")
|
||||
continue
|
||||
}
|
||||
var n = 1
|
||||
var j = BigInt.new(i)
|
||||
var h = [j]
|
||||
while (true) {
|
||||
var k = BigInt.primeFactors(j).reduce("") { |acc, f| acc + f.toString }
|
||||
j = BigInt.new(k)
|
||||
h.add(j)
|
||||
if (j.isProbablePrime(2)) {
|
||||
for (l in n...0) System.write("HP%(h[n-l])(%(l)) = ")
|
||||
System.print(h[n])
|
||||
break
|
||||
} else {
|
||||
n = n + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Task/Home-primes/Wren/home-primes-2.wren
Normal file
28
Task/Home-primes/Wren/home-primes-2.wren
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* home_primes_gmp.wren */
|
||||
|
||||
import "./gmp" for Mpz
|
||||
import "./math" for Int
|
||||
|
||||
var list = (2..20).toList
|
||||
list.add(65)
|
||||
for (i in list) {
|
||||
if (Int.isPrime(i)) {
|
||||
System.print("HP%(i) = %(i)")
|
||||
continue
|
||||
}
|
||||
var n = 1
|
||||
var j = Mpz.from(i)
|
||||
var h = [j.copy()]
|
||||
while (true) {
|
||||
var k = Mpz.primeFactors(j).reduce("") { |acc, f| acc + f.toString }
|
||||
j = Mpz.fromStr(k)
|
||||
h.add(j)
|
||||
if (j.probPrime(15) > 0) {
|
||||
for (l in n...0) System.write("HP%(h[n-l])(%(l)) = ")
|
||||
System.print(h[n])
|
||||
break
|
||||
} else {
|
||||
n = n + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue