How to resolve the algorithm Magnanimous numbers step by step in the Lambdatalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Magnanimous numbers step by step in the Lambdatalk programming language

Table of Contents

Problem Statement

A magnanimous number is an integer where there is no place in the number where a + (plus sign) could be added between any two digits to give a non-prime sum.

Traditionally the single digit numbers 0 through 9 are included as magnanimous numbers as there is no place in the number where you can add a plus between two digits at all. (Kind of weaselly but there you are...) Except for the actual value 0, leading zeros are not permitted. Internal zeros are fine though, 1001 -> 1 + 001 (prime), 10 + 01 (prime) 100 + 1 (prime). There are only 571 known magnanimous numbers. It is strongly suspected, though not rigorously proved, that there are no magnanimous numbers above 97393713331910, the largest one known.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magnanimous numbers step by step in the Lambdatalk programming language

Source code in the lambdatalk programming language

{def isprime
 {def isprime.loop
  {lambda {:n :m :i}
   {if {> :i :m}
    then true
    else {if {= {% :n :i} 0}
    then false
    else {isprime.loop :n :m {+ :i 2}}
 }}}}
 {lambda {:n}
  {if {= :n 1}
   then false
   else {if {or {= :n 2} {= :n 3} {= :n 5} {= :n 7}}
   then true
   else {if {or {< : n 2} {= {% :n 2} 0}}
   then false
   else {isprime.loop :n {sqrt :n} 3} 
}}}}}
-> isprime

{def magnanimous
 {def magnanimous.loop
  {lambda {:n :p :r}
   {if {>= :n 10}    
    then {let { {:n {floor {/ :n 10}}}
                {:p :p}
                {:r {+ :r {* {% :n 10} :p}}}
              } {if {not {isprime {+ :n :r}}}
                 then false  
                 else {magnanimous.loop :n {* :p 10} :r} } 
         }
    else true }}}
 {lambda {:n}
  {magnanimous.loop :n 1 0} }}
-> magnanimous 
 
{def mags 
 {lambda {:n}
  {S.last
   {S.map {{lambda {:a :i}
           {if {magnanimous :i} then {A.addlast! :i :a} else}} 
          {A.new}}
        {S.serie 0 :n}}}}}
-> mags 

{A.slice 0 45 {mags 110}}
-> [0,1,2,3,4,5,6,7,8,9,11,12,14,16,20,21,23,25,29,30,32,34,38,41,43,47,49,50,52,56,58,61,65,67,70,74,76,83,85,89,92,94,98,101,110]   
   
{A.slice 240 250 {mags 30000}}
-> 
[17992,19972,20209,20261,20861,22061,22201,22801,22885,24407]

{A.slice 390 400 {mags 700000}} 
-> 
[486685,488489,515116,533176,551558,559952,595592,595598,600881,602081]

time of CPU in milliseconds
         iPadPro  MacBookAir  MacBookPro
[0,45] 	      30 	      15 	      12
[240,250] 	2430 	    5770 	    3650
[390,400] 117390 	  284230 	  213210


  

You may also check:How to resolve the algorithm Even or odd step by step in the 11l programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Pascal programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Go programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Pointless programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Modula-2 programming language