Skip to content

Instantly share code, notes, and snippets.

@hugmouse
Created February 12, 2020 15:32
Show Gist options
  • Save hugmouse/732bd810af25c417301686d810c3af5e to your computer and use it in GitHub Desktop.
Save hugmouse/732bd810af25c417301686d810c3af5e to your computer and use it in GitHub Desktop.
FizzBuzz task, but the input of which is a number that is larger than the machine word (https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic)
package main
import (
"fmt"
"math/big"
)
func FizzBuzz(n string) string {
var i, x, one, zero, fizz, buzz, result big.Int
// Of course, this makes the function automatically limited to 64 bits,
// but I was too lazy to make additional logic
var amountOfFizzBuzz, amountOfFizz, amountOfBuzz int64
fizz.SetInt64(3) // % 3
buzz.SetInt64(5) // % 5
one.SetInt64(1) // ++
zero.SetInt64(0) // == 0
x.SetString(n, 10)
for i.Cmp(&x) <= 0 {
i.Add(&i, &one)
if result.Mod(&i, &fizz).Cmp(&zero) == 0 && result.Mod(&i, &buzz).Cmp(&zero) == 0 {
amountOfFizzBuzz++
} else if result.Mod(&i, &fizz).Cmp(&zero) == 0 {
amountOfFizz++
} else if result.Mod(&i, &buzz).Cmp(&zero) == 0 {
amountOfBuzz++
}
}
return fmt.Sprintf("FizzBuzz: %d\nFizz: %d\nBuzz: %d", amountOfFizzBuzz, amountOfFizz, amountOfBuzz)
}
func main() {
print(FizzBuzz("92233720"))
}
// Example output:
// FizzBuzz: 6148914
// Fizz: 24595659
// Buzz: 12297830
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment