monicaeverett

fibo_monica

Oct 9th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.67 KB | Source Code | 0 0
  1. //1.create file name : main.go 2.paste the code 3. run : go run main.go | don't forget to download go : https://go.dev/
  2.  
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "math"
  8. )
  9.  
  10. func fibonacci(n int) int64 {
  11.     if n <= 0 {
  12.         return 0
  13.     }
  14.     sqrt5 := math.Sqrt(5)
  15.     phi := (1 + sqrt5) / 2
  16.     psi := (1 - sqrt5) / 2
  17.     //สูตร (φ^n - ψ^n) / √5
  18.     result := (math.Pow(phi, float64(n)) - math.Pow(psi, float64(n))) / sqrt5
  19.     return int64(math.Round(result))
  20. }
  21.  
  22. func main() {
  23.     for {
  24.         fmt.Print("Enter value: ")
  25.         var num int
  26.         if _, err := fmt.Scanln(&num); err != nil || num <= 0 {
  27.             break
  28.         }
  29.         answer := fibonacci(num)
  30.  
  31.         fmt.Printf("Result of %d is: %d\n", num, answer)
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment