Решение на Разлика в сумите от Севда Ганева

Обратно към всички решения

Към профила на Севда Ганева

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)

Код

// Package main contains Sevda Ganeva's Homework 01
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
// SquareSumDifference calculates the difference between the square of the sum and the sum of the squares of the first n positive whole numbers
// or 0 when input is 0 or the result is out of range which is when n > 92681.
func SquareSumDifference(n uint64) uint64 {
// The result can be calculated using the following expression
// (n - 1) * n * (n + 1) * (3*n + 2) / 12 which is a growing function.
// BUG(sevda): For n > 92681 the result is outside of the return type's range. In that case 0 is returned
if n <= 0 || (n > 92681) {
return 0
}
var m1, m2, m3, m4 uint64
m1 = n - 1
m2 = n
m3 = n + 1
m4 = 3*n + 2
// if not carefull with the devision we end up either with result overflowing earlier then expected or with wrong result
if n%2 == 0 {
m2 /= 2
m4 /= 2
} else {
m1 /= 2
m3 /= 2
}
switch n % 3 {
case 0:
m2 /= 3
case 1:
m1 /= 3
default:
m3 /= 3
}
return m1 * m2 * m3 * m4
}
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("\nEnter value for n. Type \"exit\" to exit: ")
input, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
if strings.Trim(input, "\r\n\t ") == "exit" {
os.Exit(0)
}
u, err := strconv.ParseUint(strings.Trim(input, "\r\n\t "), 10, 64)
if err != nil {
//log.Fatal(err)
fmt.Println("Invalid input!")
continue
}
u = SquareSumDifference(u)
if u == 0 {
fmt.Println("Result out of range!")
} else {
fmt.Println("SquareSumDifference:", u)
}
}
}

Лог от изпълнението

PASS
ok  	_/tmp/d20161101-5193-v7nv8w	0.003s
PASS
ok  	_/tmp/d20161101-5193-v7nv8w	0.003s
PASS
ok  	_/tmp/d20161101-5193-v7nv8w	0.002s
PASS
ok  	_/tmp/d20161101-5193-v7nv8w	0.002s

История (1 версия и 0 коментара)

Севда обнови решението на 30.10.2016 13:49 (преди над 1 година)

+// Package main contains Sevda Ganeva's Homework 01
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+// SquareSumDifference calculates the difference between the square of the sum and the sum of the squares of the first n positive whole numbers
+// or 0 when input is 0 or the result is out of range which is when n > 92681.
+func SquareSumDifference(n uint64) uint64 {
+
+ // The result can be calculated using the following expression
+ // (n - 1) * n * (n + 1) * (3*n + 2) / 12 which is a growing function.
+ // BUG(sevda): For n > 92681 the result is outside of the return type's range. In that case 0 is returned
+ if n <= 0 || (n > 92681) {
+ return 0
+ }
+
+ var m1, m2, m3, m4 uint64
+ m1 = n - 1
+ m2 = n
+ m3 = n + 1
+ m4 = 3*n + 2
+
+ // if not carefull with the devision we end up either with result overflowing earlier then expected or with wrong result
+
+ if n%2 == 0 {
+ m2 /= 2
+ m4 /= 2
+ } else {
+ m1 /= 2
+ m3 /= 2
+ }
+
+ switch n % 3 {
+ case 0:
+ m2 /= 3
+ case 1:
+ m1 /= 3
+ default:
+ m3 /= 3
+ }
+
+ return m1 * m2 * m3 * m4
+
+}
+
+func main() {
+
+ reader := bufio.NewReader(os.Stdin)
+ for {
+ fmt.Print("\nEnter value for n. Type \"exit\" to exit: ")
+ input, err := reader.ReadString('\n')
+ if err != nil {
+ log.Fatal(err)
+ }
+ if strings.Trim(input, "\r\n\t ") == "exit" {
+ os.Exit(0)
+ }
+ u, err := strconv.ParseUint(strings.Trim(input, "\r\n\t "), 10, 64)
+ if err != nil {
+ //log.Fatal(err)
+ fmt.Println("Invalid input!")
+ continue
+ }
+ u = SquareSumDifference(u)
+ if u == 0 {
+ fmt.Println("Result out of range!")
+ } else {
+ fmt.Println("SquareSumDifference:", u)
+ }
+ }
+}