How to resolve the algorithm Almost prime step by step in the EchoLisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Almost prime step by step in the EchoLisp programming language
Table of Contents
Problem Statement
A k-Almost-prime is a natural number
n
{\displaystyle n}
that is the product of
k
{\displaystyle k}
(possibly identical) primes.
1-almost-primes, where
k
1
{\displaystyle k=1}
, are the prime numbers themselves. 2-almost-primes, where
k
2
{\displaystyle k=2}
, are the semiprimes.
Write a function/method/subroutine/... that generates k-almost primes and use it to create a table here of the first ten members of k-Almost primes for
1 <= K <= 5
{\displaystyle 1<=K<=5}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Almost prime step by step in the EchoLisp programming language
Source code in the echolisp programming language
(define (almost-prime? p k)
(= k (length (prime-factors p))))
(define (almost-primes k nmax)
(take (filter (rcurry almost-prime? k) [2 ..]) nmax))
(define (task (kmax 6) (nmax 10))
(for ((k [1 .. kmax]))
(write 'k= k '|)
(for-each write (almost-primes k nmax))
(writeln)))
(task)
k= 1 | 2 3 5 7 11 13 17 19 23 29
k= 2 | 4 6 9 10 14 15 21 22 25 26
k= 3 | 8 12 18 20 27 28 30 42 44 45
k= 4 | 16 24 36 40 54 56 60 81 84 88
k= 5 | 32 48 72 80 108 112 120 162 168 176
(lib 'match)
(define-syntax-rule (: v i) (vector-ref v i))
(reader-infix ':) ;; abbrev (vector-ref v i) === [v : i]
(lib 'bigint)
(define cprimes (list->vector (primes 10000)))
;; generates next k-almost-prime < pmax
;; c = vector of k primes indices c[i] <= c[j]
;; p = vector of intermediate products prime[c[0]]*prime[c[1]]*..
;; p[k-1] is the generated k-almost-prime
;; increment one c[i] at each step
(define (almost-next pmax k c p)
(define almost-prime #f)
(define cp 0)
(for ((i (in-range (1- k) -1 -1))) ;; look backwards for c[i] to increment
(vector-set! c i (1+ [c : i])) ;; increment c[i]
(set! cp [cprimes : [c : i]])
(vector-set! p i (if (> i 0) (* [ p : (1- i)] cp) cp)) ;; update partial product
(when (< [p : i) pmax)
(set! almost-prime
(and ;; set followers to c[i] value
(for ((j (in-range (1+ i) k)))
(vector-set! c j [c : i])
(vector-set! p j (* [ p : (1- j)] cp))
#:break (>= [p : j] pmax) => #f )
[p : (1- k)]
) ;; // and
) ;; set!
) ;; when
#:break almost-prime
) ;; // for i
almost-prime )
;; not sorted list of k-almost-primes < pmax
(define (almost-primes k nmax)
(define base (expt 2 k)) ;; first one is 2^k
(define pmax (* base nmax))
(define c (make-vector k #0))
(define p (build-vector k (lambda(i) (expt #2 (1+ i)))))
(cons base
(for/list
((almost-prime (in-producer almost-next pmax k c p )))
almost-prime)))
;; we want 500-almost-primes from the 10000-th.
(take (drop (list-sort < (almost-primes 500 10000)) 10000 ) 10)
(7241149198492252834202927258094752774597239286103014697435725917649659974371690699721153852986
440733637405206125678822081264723636566725108094369093648384
etc ...
;; The first one is 2^497 * 3 * 17 * 347 , same result as Haskell.
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Number names step by step in the D programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Range extraction step by step in the DWScript programming language