Home primes: Difference between revisions

Added Python implementation for the Home Primes task
(Added Python implementation for the Home Primes task)
Line 833:
= 1381_3211183211_75157763339900357651 [13.9s]
</pre>
=={{header|Python}}==
 
Abhorrently Slow, but it works.
<syntaxhighlight lang="python">
# home_primes.py by Xing216
def primeFactors(n: int) -> list[int]:
primeFactorsL = []
while n % 2 == 0:
primeFactorsL.append(2)
n = n // 2
for i in range(3,int(n**0.5)+1,2):
while n % i== 0:
primeFactorsL.append(i)
n = n // i
if n > 2:
primeFactorsL.append(n)
return primeFactorsL
def list_to_int(l: list[int]) -> int:
return int(''.join(str(i) for i in l))
def home_prime_chain(i:int) -> list[int]:
pf_int = i
chain = []
while True:
pf = primeFactors(pf_int)
pf_int = list_to_int(pf)
if len(pf) == 1:
return chain
else:
chain.append(pf_int)
for i in range(2,21):
chain_list = home_prime_chain(i)
chain_len = len(chain_list)
chain_idx_list = list(range(chain_len))[::-1]
j = chain_len
if chain_list != []:
print(f"HP{i}({chain_len}) =", end=" ")
for k,l in list(zip(chain_list, chain_idx_list)):
if l == 0:
print(f"{k}")
else:
print(f"HP{k}({l}) =", end=" ")
else:
print(f"HP{i}(1) = {i}")
</syntaxhighlight>
{{out}}
<pre style="height: 10em">
HP2(1) = 2
HP3(1) = 3
HP4(2) = HP22(1) = 211
HP5(1) = 5
HP6(1) = 23
HP7(1) = 7
HP8(13) = HP222(12) = HP2337(11) = HP31941(10) = HP33371313(9) = HP311123771(8) = HP7149317941(7) = HP22931219729(6) = HP112084656339(5) = HP3347911118189(4) = HP11613496501723(3) = HP97130517917327(2) = HP531832651281459(1) = 3331113965338635107
HP9(2) = HP33(1) = 311
HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773
HP11(1) = 11
HP12(1) = 223
HP13(1) = 13
HP14(5) = HP27(4) = HP333(3) = HP3337(2) = HP4771(1) = 13367
HP15(4) = HP35(3) = HP57(2) = HP319(1) = 1129
HP16(4) = HP2222(3) = HP211101(2) = HP3116397(1) = 31636373
HP17(1) = 17
HP18(1) = 233
HP19(1) = 19
HP20(15) = HP225(14) = HP3355(13) = HP51161(12) = HP114651(11) = HP3312739(10) = HP17194867(9) = HP194122073(8) = HP709273797(7) = HP39713717791(6) = HP113610337981(5) = HP733914786213(4) = HP3333723311815403(3) = HP131723655857429041(2) = HP772688237874641409(1) = 3318308475676071413
</pre>
=={{header|Raku}}==
 
34

edits