Skip to content

Instantly share code, notes, and snippets.

@zoracon
Last active September 25, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoracon/fb8f461c0e138ba2d2028599b68f15e3 to your computer and use it in GitHub Desktop.
Save zoracon/fb8f461c0e138ba2d2028599b68f15e3 to your computer and use it in GitHub Desktop.
HENNGE Challenge
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
)
var sum []int // sums array
var cases []int // cases array
func firstLoop(tn int) []int {
if tn == 0 {
fmt.Println("Okay, here are the sums! ")
// All test cases done, return sum
fmt.Println(sum)
return sum
}
// Make a seed for random # generation
t1 := rand.NewSource(time.Now().UnixNano())
u1 := rand.New(t1)
// Based off test case total, generate number for that case
tnew := u1.Intn(100)
// second loop that will generate squared sums
fmt.Println("Type " + strconv.Itoa(tnew) + " numbers")
sum = append(sum, sqsums(tnew, 0))
// next in line
return firstLoop(tn - 1)
}
// add squares
func sqsums(n int, nn int) int {
if n == 0 {
// clear cases slice for next batch if available
cases = nil
return nn
}
// Disregard -int and 0
if n > 0 {
var new int
// Scan in input
_, err := fmt.Scanf("%d", &new)
if err != nil {
fmt.Println("input case error")
}
// append cases
cases = append(cases, new)
if new >= 0 {
nn += new * new
}
}
return sqsums(n-1, nn)
}
func main() {
var testcases int
// Scan in input
_, err := fmt.Scanf("%d", &testcases)
if err != nil {
fmt.Println("input test case amount error")
}
// Recursion city
firstLoop(testcases)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment