• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest May 23rd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. package main
2.
3. import (
4.   "fmt"
5.   "math"
6. )
7.
8. func squareOfSum(n float64) float64 {
9.   /*
10.    *   n (n + 1)
11.    * ( --------- ) ** 2
12.    *       2
13.    */
14.   n = ((n * (n + 1)) / 2)
15.
16.   n = math.Pow(math.Floor(n), 2)
17.
18.   return n
19. }
20.
21. func sumOfSquare(n float64) float64 {
22.   /*
23.    *   n (n + 1) (2n + 1)
24.    * ( ----------------- )
25.    *            6
26.    */
27.    n = (n * (n + 1) * (2 * n + 1)) / 6
28.
29.    return n
30. }
31.
32. func diff (n float64) float64 {
33.   /*
34.    * Square of sum - sum of square of sum
35.    */
36.
37.   diff := squareOfSum(n) - sumOfSquare(n)
38.
39.   return diff
40. }
41.
42. func floatToString (n float64) string {
43.   n = math.Round(n)
44.
45.   str := fmt.Sprintf("%v", n)
46.
47.   return str
48. }
49.
50. func main() {
51. fmt.Println("Differentiate V1.0")
52.
53.   fmt.Println("A tool to obtain the difference between the square of the sum and the sum of the squares of a given natural number")
54.
55.   fmt.Println("Built by Paul Adams, 4DAS\n")
56.
57.   for {
58.     /*
59.      * Collect input
60.      */
61.     var n float64
62.     fmt.Println("What number should I compare?")
63.     fmt.Scan(&n)
64.
65.     diff := floatToString(math.Floor(diff(n)))
66.
67.     fmt.Println("\nThe difference is " + diff + "\n")
68.   }
69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top