-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-test-fibo.cpp
122 lines (105 loc) · 3.4 KB
/
4-test-fibo.cpp
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
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
int main()
{
// --- Input ---------------------------------
printf("-----------------------------------------------\n");
printf("--- *** Find Fibonacci Numbers < Abs(N) *** ---\n");
printf("-----------------------------------------------\n");
printf("\n");
int N; printf("Input (integer) N: "); cin >> N; // число, до которого строить последовательность чисел Фибоначии
int F1 = 0; int F2 = 1; int F3; double S; double k;
switch(N)
{
case 0:
printf("-----------------------------------------------\n");
printf("Fibo Array: %d \n", F1); printf("Summ Array: 0 \n");
printf("Elem Count: 1 \n");
break;
case 1:
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1); printf(", %d",F2); S = F1+F2;
printf("\nSumm Array: %.0f \n", S); printf("Elem Count: 2 \n");
break;
case 2:
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1);
printf(", %d",F2); S = F1+F2; k = 2; F2 = F1+F2;
printf(", %d",F2); S = S+F2; k++; F1 = F2; F2 = F1+F2;
printf(", %d",F2); S = S+F2; k++;
printf("\nSumm Array: %.0f \n", S);
printf("Elem Count: %.0f \n", k);
break;
case -1:
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1); printf(", %d",F2); S = F1+F2;
printf("\nSumm Array: %.0f \n", S); printf("Elem Count: 2 \n");
break;
case -2:
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1);
printf(", %d",F2); S = F1+F2; k = 2; F2 = F1-F2;
printf(", %d",F2); S = S+F2; k++; F1 = abs(F2); F2 = F1-F2;
printf(", %d",F2); S = S+F2; k++;
printf("\nSumm Array: %.0f \n", S);
printf("Elem Count: %.0f \n", k);
break;
default:
break;
}
if (N > 2) {
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1);
printf(", %d",F2); S = F1+F2; k = 2;
for (int i = 1; i <= N; i++)
{
F3 = F1 + F2;
if (F3 <= N) {
F1 = F2; F2 = F3;
printf(", %d",F2); S = S+F2; k++;
}
}
printf("\nSumm Array: %.0f \n", S);
printf("Elem Count: %.0f \n", k);
}
if (N < -2) {
printf("-----------------------------------------------\n");
printf("Fibo Array: %d",F1);
printf(", %d",F2); S = F1+F2; k = 2;
for (int i = 1; i <= -N; i++)
{
F3 = F1 - F2;
if (abs(F3) <= abs(N)) {
F1 = F2; F2 = F3;
printf(", %d",F2); S = S+F2; k++;
}
}
printf("\nSumm Array: %.0f \n", S);
printf("Elem Count: %.0f \n", k);
}
S = sqrt(N*N*(5.0)-4.0);
double M = S - (int)S;
k = sqrt(N*N*(5.0)+4.0);
double J = k - (int)k;
printf("-----------------------------------------------\n");
if ((M > 0) && (J > 0)) {
printf("Inputed N = %d",N); printf(" is NOT Fibonacci Number.\n");
} else {
if (M == 0) {
printf("Inputed N = %d",N); printf(" belongs to Fibonacci Array (-).\n");
printf("Condition: 5N^2-4 = %.0f", S*S); printf(", Sqrt* = %.2f\n", S);
}
if (J == 0) {
printf("Inputed N = %d",N); printf(" belongs to Fibonacci Array (+).\n");
printf("Condition: 5N^2+4 = %.0f", k*k); printf(", Sqrt* = %.2f\n", k);
}
}
getch();
return 0;
}