I have written the phi function in C++, but my program crashes if I declare the array phi with 1.000.000 elements. It does not give me any errors and works fine if I reduce it to 99.999 elements. Can someone suggest me any optimization to the code? It is all school-related.
#include <iostream>
using namespace std;
int main()
{
int M;
cin >> M;
int phi[1000000];
for (int i = 1; i <= M; ++i)
phi[i] = i-1;
for (int i = 2; i <= M; ++i)
for (int j = 2*i; j <= M; j += i)
phi[j] -= phi[i];
cout << phi[M];
}