How to resolve the algorithm Home primes step by step in the Factor programming language
How to resolve the algorithm Home primes step by step in the Factor programming language
Table of Contents
Problem Statement
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: If the integer obtained by concatenating increasing prime factors is not prime, iterate until you reach a prime number; the home prime.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Home primes step by step in the Factor programming language
Source code in the factor programming language
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
You may also check:How to resolve the algorithm Yin and yang step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the REBOL programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Tcl programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Yorick programming language