How to resolve the algorithm Smarandache prime-digital sequence step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smarandache prime-digital sequence step by step in the Factor programming language

Table of Contents

Problem Statement

The Smarandache prime-digital sequence (SPDS for brevity) is the sequence of primes whose digits are themselves prime. For example 257 is an element of this sequence because it is prime itself and its digits: 2, 5 and 7 are also prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smarandache prime-digital sequence step by step in the Factor programming language

Source code in the factor programming language

USING: combinators.short-circuit io lists lists.lazy math
math.parser math.primes prettyprint sequences ;
IN: rosetta-code.smarandache-naive

: smarandache? ( n -- ? )
    {
        [ number>string string>digits [ prime? ] all? ]
        [ prime? ]
    } 1&& ;

: smarandache ( -- list ) 1 lfrom [ smarandache? ] lfilter ;

: smarandache-demo ( -- )
    "First 25 members of the Smarandache prime-digital sequence:"
    print 25 smarandache ltake list>array .
    "100th member: " write smarandache 99 [ cdr ] times car . ;

MAIN: smarandache-demo


USING: combinators generalizations io kernel math math.functions
math.primes prettyprint sequences ;
IN: rosetta-code.smarandache

! Observations:
! * For 2-digit numbers and higher, only 3 and 7 are viable in
!   the ones place.
! * Only 2, 3, 5, and 7 are viable anywhere else.
! * It is possible to use this information to drastically
!   reduce the amount of numbers to check for primality.
! * For instance, by these rules we can tell that the next
!   potential Smarandache prime digital after 777 is 2223.

: next-one ( n -- n' ) 3 = 7 3 ? ; inline

: next-ten ( n -- n' )
    { { 2 [ 3 ] } { 3 [ 5 ] } { 5 [ 7 ] } [ drop 2 ] } case ;

: inc ( seq quot: ( n -- n' ) -- seq' )
    [ 0 ] 2dip [ change-nth ] curry keep ; inline

: inc1  ( seq -- seq' ) [ next-one ] inc ;
: inc10 ( seq -- seq' ) [ next-ten ] inc ;

: inc-all ( seq -- seq' )
    inc1 [ zero? not [ next-ten ] when ] V{ } map-index-as ;

: carry ( seq -- seq' )
    dup [ 7 = not ] find drop {
        { 0 [ inc1 ] }
        { f [ inc-all 2 suffix! ] }
        [ cut [ inc-all ] [ inc10 ] bi* append! ]
    } case ;

: digits>integer ( seq -- n ) [ 10 swap ^ * ] map-index sum ;

: next-smarandache ( seq -- seq' )
    [ digits>integer prime? ] [ carry dup ] do until ;

: .sm ( seq -- ) <reversed> [ pprint ] each nl ;

: first25 ( -- )
    2 3 5 7 [ . ] 4 napply V{ 7 } clone
    21 [ next-smarandache dup .sm ] times drop ;

: nth-smarandache ( n -- )
    4 - V{ 7 } clone swap [ next-smarandache ] times .sm ;

: smarandache-demo ( -- )
    "First 25 members of the Smarandache prime-digital sequence:"
    print first25 nl { 100 1000 10000 100000 } [
        dup pprint "th member: " write nth-smarandache
    ] each ;

MAIN: smarandache-demo


  

You may also check:How to resolve the algorithm Strong and weak primes step by step in the Nim programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the Python programming language
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the COBOL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Julia programming language
You may also check:How to resolve the algorithm Simple database step by step in the Raku programming language