How to resolve the algorithm Magnanimous numbers step by step in the Go programming language
How to resolve the algorithm Magnanimous numbers step by step in the Go 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 Go programming language
The provided Go code is designed to identify and list "magnanimous" numbers within a specified range. Magnanimous numbers are defined as follows:
- Magnanimous numbers must be greater than or equal to 10.
- For a magnanimous number n, the sum of its digits (p) and the remainder when divided by 10 (q) must form a prime number.
- This condition must hold for all possible "p" and "q" values as you repeatedly divide n by 10.
The code addresses this task through the following key functions:
-
isPrime(n)
: Checks if a given numbern
is prime. It employs a simple optimization to handle common prime numbers less than 10. For larger numbers, it employs a loop to check for divisibility up to the square root ofn
. -
ord(n)
: Converts a numbern
to its ordinal representation (e.g., 1st, 2nd, 3rd, etc.). -
isMagnanimous(n)
: Determines ifn
is a magnanimous number by repeatedly dividing it by 10, checking if the sum of digits and the remainder form a prime number, and stopping when the quotient becomes less than 10. -
listMags(from, thru, digs, perLine)
: Lists magnanimous numbers within a specified range. It iterates through numbers, checks for magnanimity, and prints them in a formatted manner, with options to customize the number of digits displayed and the number of values per line.
In the main
function, the program showcases the functionality by listing magnanimous numbers in specific ranges:
- From 1 to 45, displayed with three digits and 15 numbers per line.
- From 241 to 250, displayed with one digit and 10 numbers per line.
- From 391 to 400, displayed with one digit and 10 numbers per line.
Overall, this code efficiently identifies and lists magnanimous numbers within specified ranges, demonstrating features such as prime number checking, ordinal number conversion, and customizable output formatting.
Source code in the go programming language
package main
import "fmt"
// OK for 'small' numbers.
func isPrime(n uint64) bool {
switch {
case n < 2:
return false
case n%2 == 0:
return n == 2
case n%3 == 0:
return n == 3
default:
d := uint64(5)
for d*d <= n {
if n%d == 0 {
return false
}
d += 2
if n%d == 0 {
return false
}
d += 4
}
return true
}
}
func ord(n int) string {
m := n % 100
if m >= 4 && m <= 20 {
return fmt.Sprintf("%dth", n)
}
m %= 10
suffix := "th"
if m < 4 {
switch m {
case 1:
suffix = "st"
case 2:
suffix = "nd"
case 3:
suffix = "rd"
}
}
return fmt.Sprintf("%d%s", n, suffix)
}
func isMagnanimous(n uint64) bool {
if n < 10 {
return true
}
for p := uint64(10); ; p *= 10 {
q := n / p
r := n % p
if !isPrime(q + r) {
return false
}
if q < 10 {
break
}
}
return true
}
func listMags(from, thru, digs, perLine int) {
if from < 2 {
fmt.Println("\nFirst", thru, "magnanimous numbers:")
} else {
fmt.Printf("\n%s through %s magnanimous numbers:\n", ord(from), ord(thru))
}
for i, c := uint64(0), 0; c < thru; i++ {
if isMagnanimous(i) {
c++
if c >= from {
fmt.Printf("%*d ", digs, i)
if c%perLine == 0 {
fmt.Println()
}
}
}
}
}
func main() {
listMags(1, 45, 3, 15)
listMags(241, 250, 1, 10)
listMags(391, 400, 1, 10)
}
You may also check:How to resolve the algorithm Integer comparison step by step in the Ada programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the Clojure programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the zkl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Groovy programming language