-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciNumberAgain.go
More file actions
71 lines (55 loc) · 1.68 KB
/
FibonacciNumberAgain.go
File metadata and controls
71 lines (55 loc) · 1.68 KB
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
package main
import (
"bufio"
"fmt"
"math/big"
"os"
"strconv"
"strings"
)
func getFibonacci(num int) *big.Int {
results := make(map[int]*big.Int, num)
results[0] = big.NewInt(0)
results[1] = big.NewInt(1)
for i := 2; i <= num; i++ {
results[i] = new(big.Int).Add(results[i-1], results[i-2])
}
return results[num]
}
func calculate(fibNum *big.Int, divider int) *big.Int {
// find sequence for `f mod divider`
var sequence []int
for i := 0; ; i++ {
fib := getFibonacci(i)
modulo := new(big.Int).Mod(fib, new(big.Int).SetInt64(int64(divider))).Int64()
// end of the sequence
if i > 1 && modulo == 1 && sequence[i-1] == 0 {
sequence = sequence[0 : i-1]
break
}
sequence = append(sequence, int(modulo))
}
newFib := new(big.Int).Mod(fibNum, new(big.Int).SetInt64(int64(len(sequence))))
fib := getFibonacci(int(newFib.Int64()))
result := new(big.Int).Mod(fib, new(big.Int).SetInt64(int64(divider)))
return result
}
// Task. Given two integers 𝑛 and 𝑚, output 𝐹𝑛 mod 𝑚 (that is, the remainder of 𝐹𝑛 when divided by 𝑚).
// Input Format. The input consists of two integers 𝑛 and 𝑚 given on the same line (separated by a space).
// Constraints. 1≤𝑛≤1014,2≤𝑚≤103.
// Output Format. Output 𝐹𝑛 mod 𝑚.
func main() {
reader := bufio.NewReader(os.Stdin)
fib, _ := reader.ReadString('\n')
fib = strings.TrimSpace(fib)
args := strings.Split(fib, " ")
fibonacci, ok := new(big.Int).SetString(args[0], 10)
if !ok {
fmt.Println("Can't cast", args[0], "to big int")
}
divider, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("Can't cast", args[1], "to int: ", err)
}
fmt.Println(calculate(fibonacci, divider))
}