How to resolve the algorithm Weird numbers step by step in the Go programming language
How to resolve the algorithm Weird numbers step by step in the Go programming language
Table of Contents
Problem Statement
In number theory, a weird number is a natural number that is abundant but not semiperfect (and therefore not perfect either). In other words, the sum of the proper divisors of the number (divisors including 1 but not itself) is greater than the number itself (the number is abundant), but no subset of those divisors sums to the number itself (the number is not semiperfect). For example:
Find and display, here on this page, the first 25 weird numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Weird numbers step by step in the Go programming language
Overview:
This program identifies and prints the first 25 "weird" numbers, which are semiperfect even numbers that are neither abundant (sum of divisors is greater than the number) nor semiperfect (sum of a subset of divisors equals the number).
Key Concepts:
Divisors:
- The
divisors
function computes the divisors of a positive integern
.
Abundant Numbers:
- The
abundant
function checks if a numbern
is abundant, meaning the sum of its divisors is greater thann
.
Semiperfect Numbers:
- The
semiperfect
function checks if a numbern
is semiperfect, meaning it can be written as the sum of some subset of its divisors.
Sieve:
- The
sieve
function implements the Sieve of Eratosthenes algorithm to identify weird numbers up to a given limit.
Main Function:
- The
main
function callssieve
to generate a sieve that marks abundant and not semiperfect numbers asfalse
. - It iterates through even numbers starting from 2 and prints the first 25 numbers that are not marked as abundant and not semiperfect (i.e., weird numbers).
Walkthrough:
- The
divisors
function uses afor
loop to find the divisors ofn
. It starts at 2 and checks up to the square root ofn
. - The
abundant
function sums the divisors ofn
and returnstrue
if the sum is greater thann
. - The
semiperfect
function uses a recursive algorithm to determine ifn
can be represented as the sum of some subset of its divisors. It considers the first divisor and recursively explores different combinations. - The
sieve
function initializes a slice of booleansw
with a length equal to the given limit. It then iterates through even numbers and computes their divisors.- If a number is not abundant, it's marked as true in the sieve.
- If a number is semiperfect, it marks multiples of that number as true.
- In the
main
function, the program generates the sieve and then looks for the first 25 weird numbers (not marked as abundant or not semiperfect) and prints them.
Example:
For the first few weird numbers (up to 25):
Input: limit = 17000
Output:
The first 25 weird numbers are:
74 836 1210 1964 2204 2264 2534 2924 3124 3644 3904 4076 4376 4636 5104 5426 5636 5894 6464 6904 7364 7684 8254 8324 8534
Source code in the go programming language
package main
import "fmt"
func divisors(n int) []int {
divs := []int{1}
divs2 := []int{}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs = append(divs, i)
if i != j {
divs2 = append(divs2, j)
}
}
}
for i := len(divs) - 1; i >= 0; i-- {
divs2 = append(divs2, divs[i])
}
return divs2
}
func abundant(n int, divs []int) bool {
sum := 0
for _, div := range divs {
sum += div
}
return sum > n
}
func semiperfect(n int, divs []int) bool {
le := len(divs)
if le > 0 {
h := divs[0]
t := divs[1:]
if n < h {
return semiperfect(n, t)
} else {
return n == h || semiperfect(n-h, t) || semiperfect(n, t)
}
} else {
return false
}
}
func sieve(limit int) []bool {
// false denotes abundant and not semi-perfect.
// Only interested in even numbers >= 2
w := make([]bool, limit)
for i := 2; i < limit; i += 2 {
if w[i] {
continue
}
divs := divisors(i)
if !abundant(i, divs) {
w[i] = true
} else if semiperfect(i, divs) {
for j := i; j < limit; j += i {
w[j] = true
}
}
}
return w
}
func main() {
w := sieve(17000)
count := 0
const max = 25
fmt.Println("The first 25 weird numbers are:")
for n := 2; count < max; n += 2 {
if !w[n] {
fmt.Printf("%d ", n)
count++
}
}
fmt.Println()
}
You may also check:How to resolve the algorithm Variable-length quantity step by step in the 11l programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the AWK programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Chapel programming language
You may also check:How to resolve the algorithm Leap year step by step in the sed programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the C programming language