/* program fibonacci the program generate fibonacci numbers and check for prime numbers Algorithm - very straightforward f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2) for n>1 January 2022 (AG) */ #include #include #include using namespace std; int main() { long long int f[100]; //using lon long int = int64 int i, j, key, N; // current time in seconds (begin calculations) time_t seconds_i; seconds_i = time (NULL); N = 100; f[1] = 0; f[2] = 1; for (i=3; i<=N; i=i+1) { f[i] = f[i-1] + f[i-2]; // break if the number exceeds 2^63, i.e. long long double {if (f[i] > pow(2,62)) {break;} }; /* check for prime numbers */ key =0; for (j=2; j< f[i-1]; j=j+1) {if (f[i] == (f[i]/j)*j) {key=1; break;} }; cout << setw(3) << i << setw(20)<< f[i]; if(key == 0) {cout << " prime" << endl;} else {cout << endl;} } // current time in seconds (end of calculations) time_t seconds_f; seconds_f = time (NULL); cout << endl << "total elapsed time = " << seconds_f - seconds_i << " seconds" << endl << endl; return 0; }