Published on

# Calculating the Sum of Squares of Integers in Go

Authors
• Name
Moch Lutfi
@kaptenupi

## Problem

### Description

`_7- We want you to calculate the sum of squares of given integers, excluding any negatives._7- The first line of the input will be an integer N (1 <= N <= 100), indicating the number of test cases to follow._7- Each of the test cases will consist of a line with an integer X (0 < X <= 100), followed by another line consisting of X number of space-separated integers Yn (-100 <= Yn <= 100)._7- For each test case, calculate the sum of squares of the integers, excluding any negatives, and print the calculated sum in the output._7- Note: There should be no output until all the input has been received._7- Note 2: Do not put blank lines between test cases solutions._7- Note 3: Take input from standard input, and output to standard output.`

### Rules

You may only use standard library packages. In addition, extra point is awarded if solution does not declare any global variables.

#### Specific rules for Go solution

• Your source code must be a single file
• Do not use any for and goto statement
`_5package main_5_5func main() {_5 ..._5}`

#### Sample Input

`_52_54_53 -1 1 14_55_59 6 -53 32 16`

#### Sample Output

`_2206_21397`

## Solution

The first solution that comes up to my mind is recursive approach to replace for loop process. In simple step will break down into:

1. Get total input number `N`
2. Get total input number inline with space separator
3. Get all number inline input, it should be same with total number defined in step 2
4. Repeat step 2 and 3 until `N`
5. Print calculated the sum of squares of integers excluding negative numbers

## Step 1

Entry point application, focus on get first integer only then call recursive `read(scanner, numTests, 0, result)` to do the iteration

main.go
`_71package main_71_71import (_71 "bufio"_71 "fmt"_71 "os"_71 "strconv"_71 "strings"_71)_71_71func main() {_71 scanner := bufio.NewScanner(os.Stdin)_71 scanner.Scan()_71 numTests, err := strconv.Atoi(scanner.Text())_71 if err != nil {_71 panic(err)_71 }_71 result := make([]int, numTests)_71 read(scanner, numTests, 0, result)_71}_71_71func FindPositiveSquareSum(input []string, n int) int {_71 if n <= 0 {_71 return 0_71 }_71_71 addition, _ := strconv.Atoi(input[n-1])_71 if addition < 0 {_71 addition = 0_71 }_71 return (FindPositiveSquareSum(input, n-1) + addition*addition)_71}_71_71func Print(input []int, n int) {_71 if n >= len(input) {_71 return_71 }_71_71 fmt.Println(input[n])_71 Print(input, n+1)_71 return_71}_71_71func readInt(scanner *bufio.Scanner) int {_71 scanner.Scan()_71 number, _ := strconv.Atoi(scanner.Text())_71 return number_71}_71_71func readMultiple(scanner *bufio.Scanner) []string {_71 scanner.Scan()_71 readline := scanner.Text()_71 return strings.Split(readline, " ")_71}_71_71func read(scanner *bufio.Scanner, numTest, counter int, result []int) {_71 if numTest == counter {_71 // print_71 Print(result, 0)_71 return_71 }_71 num := readInt(scanner)_71 strNum := readMultiple(scanner)_71 if num != len(strNum) {_71 read(scanner, numTest, counter, result)_71 return_71 }_71_71 result[counter] = FindPositiveSquareSum(strNum, len(strNum))_71 read(scanner, numTest, counter+1, result)_71}`

## Step 3

Read input as described in solution step above, line 62 is step 2 and the rest is step 3

main.go
`_71package main_71_71import (_71 "bufio"_71 "fmt"_71 "os"_71 "strconv"_71 "strings"_71)_71_71func main() {_71 scanner := bufio.NewScanner(os.Stdin)_71 scanner.Scan()_71 numTests, err := strconv.Atoi(scanner.Text())_71 if err != nil {_71 panic(err)_71 }_71 result := make([]int, numTests)_71 read(scanner, numTests, 0, result)_71}_71_71func FindPositiveSquareSum(input []string, n int) int {_71 if n <= 0 {_71 return 0_71 }_71_71 addition, _ := strconv.Atoi(input[n-1])_71 if addition < 0 {_71 addition = 0_71 }_71 return (FindPositiveSquareSum(input, n-1) + addition*addition)_71}_71_71func Print(input []int, n int) {_71 if n >= len(input) {_71 return_71 }_71_71 fmt.Println(input[n])_71 Print(input, n+1)_71 return_71}_71_71func readInt(scanner *bufio.Scanner) int {_71 scanner.Scan()_71 number, _ := strconv.Atoi(scanner.Text())_71 return number_71}_71_71func readMultiple(scanner *bufio.Scanner) []string {_71 scanner.Scan()_71 readline := scanner.Text()_71 return strings.Split(readline, " ")_71}_71_71func read(scanner *bufio.Scanner, numTest, counter int, result []int) {_71 if numTest == counter {_71 // print_71 Print(result, 0)_71 return_71 }_71 num := readInt(scanner)_71 strNum := readMultiple(scanner)_71 if num != len(strNum) {_71 read(scanner, numTest, counter, result)_71 return_71 }_71_71 result[counter] = FindPositiveSquareSum(strNum, len(strNum))_71 read(scanner, numTest, counter+1, result)_71}`

## Step 4

Calculate positive square sum then store it in array with `counter` index.

main.go
`_71package main_71_71import (_71 "bufio"_71 "fmt"_71 "os"_71 "strconv"_71 "strings"_71)_71_71func main() {_71 scanner := bufio.NewScanner(os.Stdin)_71 scanner.Scan()_71 numTests, err := strconv.Atoi(scanner.Text())_71 if err != nil {_71 panic(err)_71 }_71 result := make([]int, numTests)_71 read(scanner, numTests, 0, result)_71}_71_71func FindPositiveSquareSum(input []string, n int) int {_71 if n <= 0 {_71 return 0_71 }_71_71 addition, _ := strconv.Atoi(input[n-1])_71 if addition < 0 {_71 addition = 0_71 }_71 return (FindPositiveSquareSum(input, n-1) + addition*addition)_71}_71_71func Print(input []int, n int) {_71 if n >= len(input) {_71 return_71 }_71_71 fmt.Println(input[n])_71 Print(input, n+1)_71 return_71}_71_71func readInt(scanner *bufio.Scanner) int {_71 scanner.Scan()_71 number, _ := strconv.Atoi(scanner.Text())_71 return number_71}_71_71func readMultiple(scanner *bufio.Scanner) []string {_71 scanner.Scan()_71 readline := scanner.Text()_71 return strings.Split(readline, " ")_71}_71_71func read(scanner *bufio.Scanner, numTest, counter int, result []int) {_71 if numTest == counter {_71 // print_71 Print(result, 0)_71 return_71 }_71 num := readInt(scanner)_71 strNum := readMultiple(scanner)_71 if num != len(strNum) {_71 read(scanner, numTest, counter, result)_71 return_71 }_71_71 result[counter] = FindPositiveSquareSum(strNum, len(strNum))_71 read(scanner, numTest, counter+1, result)_71}`

## Step 5

Call `read` recursively with `counter+1`

main.go
`_71package main_71_71import (_71 "bufio"_71 "fmt"_71 "os"_71 "strconv"_71 "strings"_71)_71_71func main() {_71 scanner := bufio.NewScanner(os.Stdin)_71 scanner.Scan()_71 numTests, err := strconv.Atoi(scanner.Text())_71 if err != nil {_71 panic(err)_71 }_71 result := make([]int, numTests)_71 read(scanner, numTests, 0, result)_71}_71_71func FindPositiveSquareSum(input []string, n int) int {_71 if n <= 0 {_71 return 0_71 }_71_71 addition, _ := strconv.Atoi(input[n-1])_71 if addition < 0 {_71 addition = 0_71 }_71 return (FindPositiveSquareSum(input, n-1) + addition*addition)_71}_71_71func Print(input []int, n int) {_71 if n >= len(input) {_71 return_71 }_71_71 fmt.Println(input[n])_71 Print(input, n+1)_71 return_71}_71_71func readInt(scanner *bufio.Scanner) int {_71 scanner.Scan()_71 number, _ := strconv.Atoi(scanner.Text())_71 return number_71}_71_71func readMultiple(scanner *bufio.Scanner) []string {_71 scanner.Scan()_71 readline := scanner.Text()_71 return strings.Split(readline, " ")_71}_71_71func read(scanner *bufio.Scanner, numTest, counter int, result []int) {_71 if numTest == counter {_71 // print_71 Print(result, 0)_71 return_71 }_71 num := readInt(scanner)_71 strNum := readMultiple(scanner)_71 if num != len(strNum) {_71 read(scanner, numTest, counter, result)_71 return_71 }_71_71 result[counter] = FindPositiveSquareSum(strNum, len(strNum))_71 read(scanner, numTest, counter+1, result)_71}`

## Step 1

Entry point application, focus on get first integer only then call recursive `read(scanner, numTests, 0, result)` to do the iteration

## Step 3

Read input as described in solution step above, line 62 is step 2 and the rest is step 3

## Step 4

Calculate positive square sum then store it in array with `counter` index.

## Step 5

Call `read` recursively with `counter+1`

main.go
`_71package main_71_71import (_71 "bufio"_71 "fmt"_71 "os"_71 "strconv"_71 "strings"_71)_71_71func main() {_71 scanner := bufio.NewScanner(os.Stdin)_71 scanner.Scan()_71 numTests, err := strconv.Atoi(scanner.Text())_71 if err != nil {_71 panic(err)_71 }_71 result := make([]int, numTests)_71 read(scanner, numTests, 0, result)_71}_71_71func FindPositiveSquareSum(input []string, n int) int {_71 if n <= 0 {_71 return 0_71 }_71_71 addition, _ := strconv.Atoi(input[n-1])_71 if addition < 0 {_71 addition = 0_71 }_71 return (FindPositiveSquareSum(input, n-1) + addition*addition)_71}_71_71func Print(input []int, n int) {_71 if n >= len(input) {_71 return_71 }_71_71 fmt.Println(input[n])_71 Print(input, n+1)_71 return_71}_71_71func readInt(scanner *bufio.Scanner) int {_71 scanner.Scan()_71 number, _ := strconv.Atoi(scanner.Text())_71 return number_71}_71_71func readMultiple(scanner *bufio.Scanner) []string {_71 scanner.Scan()_71 readline := scanner.Text()_71 return strings.Split(readline, " ")_71}_71_71func read(scanner *bufio.Scanner, numTest, counter int, result []int) {_71 if numTest == counter {_71 // print_71 Print(result, 0)_71 return_71 }_71 num := readInt(scanner)_71 strNum := readMultiple(scanner)_71 if num != len(strNum) {_71 read(scanner, numTest, counter, result)_71 return_71 }_71_71 result[counter] = FindPositiveSquareSum(strNum, len(strNum))_71 read(scanner, numTest, counter+1, result)_71}`