How to resolve the algorithm Equal prime and composite sums step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Equal prime and composite sums step by step in the Quackery programming language

Table of Contents

Problem Statement

Suppose we have a sequence of prime sums, where each term Pn is the sum of the first n primes.

Further; suppose we have a sequence of composite sums, where each term Cm is the sum of the first m composites.

Notice that the third term of P; P3 (10) is equal to the second term of C; C2 (10);

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Equal prime and composite sums step by step in the Quackery programming language

Source code in the quackery programming language

  [ swap number$
    tuck size -
    space swap of
    swap join echo$ ]         is r-echo      ( n n --> $ )

  [ stack ]                   is primecount  (     --> s )
  [ stack ]                   is recentprime (     --> s )
  [ stack ]                   is primesum    (     --> s )
  [ stack ]                   is compcount   (     --> s )
  [ stack ]                   is recentcomp  (     --> s )
  [ stack ]                   is compsum     (     --> s )

  [ recentprime take
    [ 1+ dup isprime iff
        [ 1 primecount tally
          dup recentprime put
          primesum tally ]
        done
     again ] ]                is nextprime   (     -->   )

  [ recentcomp take
    [ 1+ dup isprime not iff
        [ 1 compcount tally
          dup recentcomp put
          compsum tally ]
        done
     again ] ]                is nextcomp    (     -->   )

  1 primecount  put
  2 recentprime put
  2 primesum    put
  1 compcount   put
  4 recentcomp  put
  4 compsum     put
  []
  [ primesum share
    compsum  share
    2dup > iff
      [ 2drop nextcomp ]
      again
    < iff nextprime again
    compsum    share
    primecount share
    compcount  share
    join join nested join
    dup size 7 < while
    nextcomp
    nextprime
    again ]
   primecount  release
   recentprime release
   primesum    release
   compcount   release
   recentcomp  release
   compsum     release
   say "        sum      prime  composite" cr
   witheach
     [ witheach
         [ 11 r-echo ]
       cr ]

  

You may also check:How to resolve the algorithm Ackermann function step by step in the BCPL programming language
You may also check:How to resolve the algorithm Compiler/AST interpreter step by step in the Python programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Benford's law step by step in the Erlang programming language
You may also check:How to resolve the algorithm Here document step by step in the Lua programming language