commit 7b54f3713eaa9dc125bd6a14639e881e529bcedf Author: Ellen Date: Thu Feb 6 21:36:15 2020 +0100 initial commit diff --git a/main.go b/main.go new file mode 100644 index 0000000..d7568fe --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "math" +) + +// NICHT FÜR MULTITHREADING GEEIGNET :( + +func main() { + fmt.Printf("%d", findNumberByBouncyPercentage(0.99)) + +} + +func returnDigit(num, place int) int { + r := num % int(math.Pow(10, float64(place))) + return r / int(math.Pow(10, float64(place-1))) +} + +func countDigits(num int) (count int) { + for num != 0 { + num /= 10 + count = count + 1 + } + return count +} + +func isBouncy(num int) bool { + var isIncreasing = true + var isDecreasing = true + for i := countDigits(num); i > 1; i-- { + if returnDigit(num, i) < returnDigit(num, i-1) { + isDecreasing = false + } else { + if returnDigit(num, i) > returnDigit(num, i-1) { + isIncreasing = false + } + } + if !(isIncreasing || isDecreasing) { + return true + } + } + return false +} + +func findNumberByBouncyPercentage(percentage float64) (num int) { + num = 1 + var bouncyCount = 0 + for { + if isBouncy(num) { + bouncyCount++ + } + var proportion = float64(bouncyCount) / float64(num) + if proportion == percentage { + return num + } else if proportion > percentage || percentage >= 1 { + fmt.Printf("no number at which the bouncy proportion is exactly %d could be found", percentage) + return num + } + num++ + } +}