How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Go programming language
How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Go programming language
Table of Contents
Problem Statement
Consider the number 24. Its proper divisors are: 1, 2, 3, 4, 6, 8 and 12. Their product is 13,824 and the cube root of this is 24. So 24 satisfies the definition in the task title. Compute and show here the first 50 positive integers which are the cube roots of the product of their proper divisors. Also show the 500th and 5,000th such numbers. Compute and show the 50,000th such number. OEIS considers 1 to be the first number in this sequence even though, strictly speaking, it has no proper divisors. Please therefore do likewise.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Go programming language
The code snippet provided is a Go program that finds the first 50,000 numbers that have 1 or 8 divisors and prints them in a table. It uses a function called divisorCount
to calculate the number of divisors for a given integer n
.
The divisorCount
function works by iterating over all the integers from 1 to the square root of n
. For each integer i
, it checks if n
is divisible by i
. If it is, then i
and n/i
are both divisors of n
. The function keeps a count of the number of divisors and returns it at the end.
The main
function starts by initializing an empty slice called numbers50
. Then, it enters a loop that iterates over all the integers starting from 1. For each integer n
, it calls the divisorCount
function to calculate the number of divisors for n
. If n
has 1 or 8 divisors, then it is added to the numbers50
slice and the count is incremented.
Once the count reaches 50, the numbers50
slice is printed in a table using the PrintTable
function from the rcu
package. The PrintTable
function takes a slice of integers, the number of columns to print, and the number of digits to pad each number with. It prints the numbers in a table format, with each number right-aligned and padded with zeros.
The main
function continues iterating over the integers until the count reaches 50,000. At that point, it prints the 500th, 5,000th, and 50,000th numbers that have 1 or 8 divisors.
The output of the program is a table of the first 50 numbers that have 1 or 8 divisors, followed by the 500th, 5,000th, and 50,000th numbers that have 1 or 8 divisors.
Source code in the go programming language
package main
import (
"fmt"
"math"
"rcu"
)
func divisorCount(n int) int {
k := 1
if n%2 == 1 {
k = 2
}
count := 0
sqrt := int(math.Sqrt(float64(n)))
for i := 1; i <= sqrt; i += k {
if n%i == 0 {
count++
j := n / i
if j != i {
count++
}
}
}
return count
}
func main() {
var numbers50 []int
count := 0
for n := 1; count < 50000; n++ {
dc := divisorCount(n)
if n == 1 || dc == 8 {
count++
if count <= 50 {
numbers50 = append(numbers50, n)
if count == 50 {
rcu.PrintTable(numbers50, 10, 3, false)
}
} else if count == 500 {
fmt.Printf("\n500th : %s", rcu.Commatize(n))
} else if count == 5000 {
fmt.Printf("\n5,000th : %s", rcu.Commatize(n))
} else if count == 50000 {
fmt.Printf("\n50,000th: %s\n", rcu.Commatize(n))
}
}
}
}
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the Wren programming language
You may also check:How to resolve the algorithm File input/output step by step in the Babel programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm String case step by step in the Delphi programming language
You may also check:How to resolve the algorithm Align columns step by step in the Forth programming language