I'm trying to get an if statement to terminate the program when the limit for a convergent sequence is met, in this case 3+(1/k^2) = 3.
#include <iostream>
#include <math.h>
int findK(int k)
{
double x = 0;
for(double i=2;i<k;i++)
{
x = (1/pow(i, 2)+3);
if(std::fmod(x, 1.0) == 0)
{
std::cout << "Sequence terminated at, " << i << "th term.\n";
exit(0);
}
else
{
std::cout << x;
}
if(i != k-1) std::cout << ", ";
}
std::cout << std::endl;
}
int main()
{
int n = 453;
findK(n);
return 0;
}
I am not the best at maths or programming/c++ but it appears to me as though once the sequence hits 3, the if statement doesn't trigger. When I replace x = (1/pow(i, 2)+3) with x = 3. Then the if statement runs and terminates the program. Am I missing something here? Please let me know in dummy terms if you can.