Just a couple of number tricks to check Go's mathematical abilities. If you have more, let's show them here.
Some number tricks (golang)
// number_tricks2.go
//
// a couple of number tricks for the fun of it
//
// 'var x int = 123' is the same as (shorthand) 'x := 123'
//
// online play at:
// https://play.golang.org/p/Vfwg6cfZhd
//
// tested with Go version 1.4.2 by vegaseat (dns) 15may2015
package main
import "fmt"
// sum integers 0 to n (inclusive)
func sum(n uint64) uint64 {
sum := uint64(0)
for x := uint64(0); x <= n; x++ {
sum += x
}
return sum
}
func main() {
fmt.Println("Old programming humor:")
fmt.Printf("Oct %o == Dec %d\n", 25, 25)
fmt.Println("-------------------------")
fmt.Println("Nine ones squared ...")
// int or int32 goes to 2147483647
// int64 goes to 9223372036854775807
var n9 int64 = 111111111
fmt.Printf("%v * %v = %v\n", n9, n9, n9*n9)
fmt.Println("(Result is 1 to 9 and down again)")
fmt.Println("-------------------------")
// magic prime 37
n := 37
for k := 3; k < 28; k += 3 {
fmt.Printf("%v * %2d = %v\n", n, k, n*k)
}
fmt.Println("-------------------------")
// 1 through 9 without the 8
mm := 12345679
// note that the digits in k sum to 9
for k := 9; k < 82; k += 9 {
fmt.Printf("%v * %2d = %v\n", mm, k, mm*k)
}
fmt.Println("-------------------------")
fmt.Println("The whole thing on steroids:")
steroid := uint64(1000000001)
for k := 9; k < 82; k += 9 {
fmt.Printf("%v\n", steroid * uint64(mm) * uint64(k))
}
fmt.Println("-------------------------")
fmt.Println("Sum 0 to a multiple of 10:")
// multiples of 10
tens := []uint64{10, 100, 1000, 10000, 100000, 1000000, 10000000}
for _, ten := range tens {
fmt.Printf("sum(%d) = %v\n", ten, sum(ten))
}
fmt.Println("-------------------------")
// pick any 2 digit integer eg. 57
p := 57
q := p
mp := []int{3, 7, 13, 37} // all primes
for _, k := range mp {
q *= k
}
fmt.Printf("You picked %d and got %d\n", p, q)
fmt.Println("-------------------------")
// pick any 5 digit integer eg. 11457
p2 := 11475
q2 := p2
mp2 := []int{11, 9091} // primes again
for _, k := range mp2 {
q2 *= k
}
fmt.Printf("You picked %d and got %d\n", p2, q2)
fmt.Println("-------------------------")
fmt.Println("Grains of rice on a chessboard if doubled each square:")
// has to be an unsigned integer for left shift to work properly
var square_number uint64 = 64
grains_on_square := uint64(1) << (square_number - 1)
sf := "..... number of grains on square number %v = %v\n"
fmt.Printf(sf, square_number, grains_on_square)
grains_total := (uint64(1) << square_number) - 1
sf2 := "total number of grains by square number %v = %v\n"
fmt.Printf(sf2, square_number, grains_total)
fmt.Printf("calculated with a loop for comparison = %v\n",
uint64(18446744073709551615))
}
/* result:
Old programming humor:
Oct 31 == Dec 25
-------------------------
Nine ones squared ...
111111111 * 111111111 = 12345678987654321
(Result is 1 to 9 and down again)
-------------------------
37 * 3 = 111
37 * 6 = 222
37 * 9 = 333
37 * 12 = 444
37 * 15 = 555
37 * 18 = 666
37 * 21 = 777
37 * 24 = 888
37 * 27 = 999
-------------------------
12345679 * 9 = 111111111
12345679 * 18 = 222222222
12345679 * 27 = 333333333
12345679 * 36 = 444444444
12345679 * 45 = 555555555
12345679 * 54 = 666666666
12345679 * 63 = 777777777
12345679 * 72 = 888888888
12345679 * 81 = 999999999
-------------------------
The whole thing on steroids:
111111111111111111
222222222222222222
333333333333333333
444444444444444444
555555555555555555
666666666666666666
777777777777777777
888888888888888888
999999999999999999
-------------------------
Sum 0 to a multiple of 10:
sum(10) = 55
sum(100) = 5050
sum(1000) = 500500
sum(10000) = 50005000
sum(100000) = 5000050000
sum(1000000) = 500000500000
sum(10000000) = 50000005000000
-------------------------
You picked 57 and got 575757
-------------------------
You picked 11475 and got 1147511475
-------------------------
Grains of rice on a chessboard if doubled each square:
..... number of grains on square number 64 = 9223372036854775808
total number of grains by square number 64 = 18446744073709551615
calculated with a loop for comparison = 18446744073709551615
*/
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.