-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitabisa.go
113 lines (102 loc) · 2.67 KB
/
kitabisa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import "os"
import "fmt"
import "strconv"
import "math"
func PrintFibonacci (noOfSeq int) []int {
var fibonacciList []int
for i := 0; i < noOfSeq; i++ {
fib := FibonacciRecursion(i)
fibonacciList = append (fibonacciList, fib)
}
return fibonacciList
}
func PrintList (numberList []int) {
for i:= 0; i < len (numberList); i++ {
fmt.Print(numberList[i])
fmt.Print(" ")
}
fmt.Print("\n")
}
func PrintPrime (noOfSeq int) []int {
var primeList []int
for i := 0; len(primeList) < noOfSeq; i++ {
if IsPrimeSqrt(i) {
primeList = append (primeList, i)
}
}
return primeList
}
func PrintMulti (firstArg, secondArg int) int {
var multiResult int
multiResult = firstArg * secondArg
return multiResult
//fmt.Println (multiResult)
}
func PrintAddition( firstArg, secondArg int) int {
var addResult int
addResult = firstArg + secondArg
return addResult
}
func main () {
argCount := len (os.Args[1:])
var calcResult int
var resultList []int
if argCount == 2 {
if noOfSeq, err:= strconv.Atoi(os.Args[1]); err == nil {
if os.Args[2] == "p" {
fmt.Println ("Run prime function")
resultList = PrintPrime(noOfSeq)
PrintList(resultList)
} else if os.Args[2] == "f" {
fmt.Println ("Run Fibonacci function")
resultList = PrintFibonacci(noOfSeq)
PrintList(resultList)
} else {
fmt.Println ("Operation must be f (Fibonnaci) or p (Prime) ")
}
} else {
fmt.Println ("First argument must be number")
}
} else if argCount == 3 {
firstArg, firstErr := strconv.Atoi(os.Args[1])
secondArg, secondErr := strconv.Atoi(os.Args[2])
//fmt.Println (os.Args[3])
if firstErr == nil && secondErr == nil {
if os.Args[3] == "x" {
calcResult = PrintMulti(firstArg, secondArg)
fmt.Println ("Run multiplication function")
fmt.Println (calcResult)
} else if os.Args[3] == "+" {
fmt.Println ("Run addition function")
calcResult = firstArg + secondArg
fmt.Println (calcResult)
} else {
fmt.Println ("Operator must be x or +")
}
} else {
fmt.Println ("First two arguments must be numbers")
}
} else {
fmt.Println ("Not enough argument")
fmt.Println ("Usage: ")
fmt.Println ("kitabisa a b + for addition")
fmt.Println ("kitabisa a b x for multiplication")
fmt.Println ("kitabisa a p for prime numbers")
fmt.Println ("kitabisa a f for Fibonacci sequence")
}
}
func FibonacciRecursion(n int) int {
if n <= 1 {
return n
}
return FibonacciRecursion(n-1) + FibonacciRecursion(n-2)
}
func IsPrimeSqrt(value int) bool {
for i := 2; i <= int(math.Floor(math.Sqrt(float64(value)))); i++ {
if value%i == 0 {
return false
}
}
return value > 1
}