forked from Krushna-Prasad-Sahoo/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci number.txt
More file actions
138 lines (110 loc) · 3.86 KB
/
Fibonacci number.txt
File metadata and controls
138 lines (110 loc) · 3.86 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
FIBONACCI NUMBERS
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
with seed values
F0 = 0 and F1 = 1.
............................................... C PROGRAM.......................................................................
// C program to check if x is a perfect square
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
// A utility function that returns true if x is perfect
// square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s * s == x);
}
// Returns true if n is a Fibonacci Number, else false
bool isFibonacci(int n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or
// both is a perfect square
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// A utility function to test above functions
int main()
{
for (int i = 1; i <= 10; i++) {
if (isFibonacci(i))
printf("%d is a Fibonacci Number \n", i);
else
printf("%d is a not Fibonacci Number \n", i);
}
return 0;
}
................................................C++ PROGRAM......................................................
// C++ program to check if x is a perfect square
#include <bits/stdc++.h>
using namespace std;
// A utility function that returns true if x is perfect
// square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s * s == x);
}
// Returns true if n is a Fibonacci Number, else false
bool isFibonacci(int n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or
// both is a perfect square
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// A utility function to test above functions
int main()
{
for (int i = 1; i <= 10; i++)
isFibonacci(i)
? cout << i << " is a Fibonacci Number \n"
: cout << i << " is a not Fibonacci Number \n";
return 0;
}
..............................................PYTHON PROGRAM.........................................................
# python program to check if x is a perfect square
import math
# A utility function that returns true if x is perfect square
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s*s == x
# Returns true if n is a Fibonacci Number, else false
def isFibonacci(n):
# n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
# is a perfect square
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
# A utility function to test above functions
for i in range(1,11):
if (isFibonacci(i) == True):
print i,"is a Fibonacci Number"
else:
print i,"is a not Fibonacci Number "
.......................................................JAVA PROGRAM.....................................................
// Java program to check if x is a perfect square
class Hactoberfest
{
// A utility method that returns true if x is perfect square
static boolean isPerfectSquare(int x)
{
int s = (int) Math.sqrt(x);
return (s*s == x);
}
// Returns true if n is a Fibonacci Number, else false
static boolean isFibonacci(int n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
// is a perfect square
return isPerfectSquare(5*n*n + 4) ||
isPerfectSquare(5*n*n - 4);
}
// Driver method
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
System.out.println(isFibonacci(i) ? i + " is a Fibonacci Number" :
i + " is a not Fibonacci Number");
}
}