Amicable Numbers - In GOlang. This is my version of Hello World for starting with GOlang. Cheerz!
Source-Code
package main
import "fmt"
import "math"
func main() {
fmt.Println("#****************************************************************#")
fmt.Println("* *")
fmt.Println("* AMICABLE NUMBERS version 0.0.1, (c.) by DaVe *")
fmt.Println("* 2019-12-30, 21:50:06 *")
fmt.Println("* *")
fmt.Println("* Welcome to AMICABLE NUMBERS in GO language, by kimhauser.ch *")
fmt.Println("* This is a CLI program for finding amicable numbers in a *")
fmt.Println("* range of Integers and print them to standard output *")
fmt.Println("* *")
fmt.Println("* https://en.wikipedia.org/wiki/Amicable_numbers *")
fmt.Println("* *")
fmt.Println("* Report any problems and feedback to This email address is being protected from spambots. You need JavaScript enabled to view it. *")
fmt.Println("* *")
fmt.Println("#****************************************************************#")
fmt.Print("Enter START number (i.e. 1): ")
var startNum int
fmt.Scanf("%d", &startNum)
fmt.Print("Enter END number (i.e. 10000): ")
var endNum int
fmt.Scanf("%d", &endNum)
var numList []int
for i := startNum; i <= endNum; i++ {
if(!contains(numList, i)){
var quer = calcSum(i)
if(quer != i){
if(i == calcSum(quer)){
numList = append(numList, quer)
fmt.Printf("%d: %d <=> %d\n", len(numList), i, quer)
}
}
}
}
fmt.Printf("Search finished. Found %d number(s) between %d and %d\n", len(numList), startNum, endNum)
}
func calcSum(num int) int {
var sum int = 1
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
if(math.Mod(float64(num), float64(i)) == 0){
sum += i + (num / i)
}
}
return sum
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
Console output
#****************************************************************#
* *
* AMICABLE NUMBERS version 0.0.1, (c.) by DaVe *
* 2019-12-30, 21:50:06 *
* *
* Welcome to AMICABLE NUMBERS in GO language, by kimhauser.ch *
* This is a CLI program for finding amicable numbers in a *
* range of Integers and print them to standard output *
* *
* https://en.wikipedia.org/wiki/Amicable_numbers *
* *
* Report any problems and feedback to This email address is being protected from spambots. You need JavaScript enabled to view it. *
* *
#****************************************************************#
Enter START number (i.e. 1): 1
Enter END number (i.e. 10000): 10000
1: 220 <=> 284
2: 1184 <=> 1210
3: 2620 <=> 2924
4: 5020 <=> 5564
5: 6232 <=> 6368
Search finished. Found 5 number(s) between 1 and 10000
Downloads