-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_primes.sf
52 lines (42 loc) · 1.95 KB
/
home_primes.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/ruby
# Find the n-th Home Prime (along with its chain).
# Definition:
# Home primes: for n >= 2, a(n) = the prime that is finally reached when you start with n, concatenate its prime factors (A037276) and repeat until a prime is reached.
# See also:
# https://oeis.org/A037274
# https://rosettacode.org/wiki/Home_primes
func home_prime(n) {
return n if (n < 2)
var steps = []
for (var f = n.factor; true; f = n.factor) {
steps << f
n = Num(f.join)
break if n.is_prime
}
return steps
}
for n in (2..20, 65) {
var steps = home_prime(n)
say ("HP(#{n}) = ", steps.map { .join('_') }.join(' -> '))
}
__END__
HP(2) = 2
HP(3) = 3
HP(4) = 2_2 -> 2_11
HP(5) = 5
HP(6) = 2_3
HP(7) = 7
HP(8) = 2_2_2 -> 2_3_37 -> 3_19_41 -> 3_3_3_7_13_13 -> 3_11123771 -> 7_149_317_941 -> 229_31219729 -> 11_2084656339 -> 3_347_911_118189 -> 11_613_496501723 -> 97_130517_917327 -> 53_1832651281459 -> 3_3_3_11_139_653_3863_5107
HP(9) = 3_3 -> 3_11
HP(10) = 2_5 -> 5_5 -> 5_11 -> 7_73
HP(11) = 11
HP(12) = 2_2_3
HP(13) = 13
HP(14) = 2_7 -> 3_3_3 -> 3_3_37 -> 47_71 -> 13_367
HP(15) = 3_5 -> 5_7 -> 3_19 -> 11_29
HP(16) = 2_2_2_2 -> 2_11_101 -> 3_11_6397 -> 3_163_6373
HP(17) = 17
HP(18) = 2_3_3
HP(19) = 19
HP(20) = 2_2_5 -> 3_3_5_5 -> 5_11_61 -> 11_4651 -> 3_3_12739 -> 17_194867 -> 19_41_22073 -> 709_273797 -> 3_97_137_17791 -> 11_3610337981 -> 7_3391_4786213 -> 3_3_3_3_7_23_31_1815403 -> 13_17_23_655857429041 -> 7_7_2688237874641409 -> 3_31_8308475676071413
HP(65) = 5_13 -> 3_3_3_19 -> 11_13_233 -> 11_101203 -> 3_3_23_53629 -> 3_3_1523_24247 -> 3_3_3_7_47_3732109 -> 11_18013_16843763 -> 151_740406071813 -> 3_13_13_54833_5458223 -> 3_3_97_179_373_7523_71411 -> 1571_1601_1350675311441 -> 3_3_13_33391_143947_279384649 -> 11_23_204069263_6417517893491 -> 7_11_1756639_83039633268945697 -> 29_29_5165653_13503983_12122544283 -> 228345060379_1282934064985326977 -> 3_3_3_2979253_3030445387_9367290955541 -> 1381_3211183211_75157763339900357651