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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Magnanimous numbers step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

#include "isprime.bas"

dim as uinteger magn(0 to 399), i, n=10, j
dim as string ns, lefty, righty
for i = 0 to 9
    magn(i) = i    'all single digit ints are magnanimous by definition
next i

while i<400
    n += 1
    ns = str(n)
    for j = 1 to len(ns)-1
        lefty = left(ns, j)
        righty = right(ns, len(ns)-j)
        if not isprime( val(lefty) + val(righty) ) then continue while
    next j
    magn(i) = n
    i+=1
wend

for i=0 to 44
    print i+1,magn(i)
next i

for i=240 to 249
    print i+1,magn(i)
next i

for i=390 to 399
    print i+1,magn(i)
next i

  

You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Vector products step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the FreeBASIC programming language