-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastDigitOfTheSumOfFibonacciNumbersAgain.go
More file actions
79 lines (62 loc) · 1.81 KB
/
LastDigitOfTheSumOfFibonacciNumbersAgain.go
File metadata and controls
79 lines (62 loc) · 1.81 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
72
73
74
75
76
77
78
79
package main
import (
"bufio"
"fmt"
"math/big"
"os"
"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 findPisanoPeriod(divider *big.Int) {
var period []int
for i := 0; ; i++ {
fib := getFibonacci(i)
modulo := new(big.Int).Mod(fib, divider).Int64()
// end of the period
if i > 1 && modulo == 1 && period[i-1] == 0 {
period = period[0 : i-1]
break
}
period = append(period, int(modulo))
}
fmt.Println("Pisano period for modulo", divider, "is:", period)
}
func findPisanoPeriodV2(from *big.Int) {
var period []int
for i := from; ; i.Add(i, new(big.Int).SetInt64(1)) {
fib := getFibonacci(i)
modulo := new(big.Int).Mod(fib, divider).Int64()
// end of the period
if i > 1 && modulo == 1 && period[i-1] == 0 {
period = period[0 : i-1]
break
}
period = append(period, int(modulo))
}
fmt.Println("Pisano period for modulo", divider, "is:", period)
}
// Task. Given two non-negative integers 𝑚 and 𝑛, where 𝑚 ≤ 𝑛, find the last digit of the sum 𝐹𝑚 + 𝐹𝑚+1 + ···+𝐹𝑛.
// Input Format. The input consists of two non-negative integers 𝑚 and 𝑛 separated by a space.
// Constraints. 0 ≤ 𝑚 ≤ 𝑛 ≤ 1014.
// Output Format. Output the last digit of 𝐹𝑚 + 𝐹𝑚+1 + · · · + 𝐹𝑛.
func main() {
reader := bufio.NewReader(os.Stdin)
fib, _ := reader.ReadString('\n')
nums := strings.Split(strings.TrimSpace(fib), " ")
if len(nums) != 2 {
fmt.Println("Provide two numbers")
return
}
from, _ := new(big.Int).SetString(nums[0], 10)
to, _ := new(big.Int).SetString(nums[1], 10)
findPisanoPeriod(from)
findPisanoPeriod(to)
}