Lerrys Posted October 28, 2015 Share Posted October 28, 2015 Programul citeste numarul natural n si determina suma primelor n patrate perfecte nenule. Il avem doar pe n ca date de intrare. Precizare: 0<n<=1000 (poate fi oricare, doar trebuie sa inlocuiti in for valorile) Am notat cu 'S' suma. #include <iostream> #include <cmath> using namespace std; int n,S=0,i,d; int main() {cin>>n; for(i=1;i<=1000000;i++) { if(n!=0) { if(i==sqrt(i*i)) S=S+i*i; n=n-1;} } cout<<"Rezultatul este "<<S; return 0; } Link to comment Share on other sites More sharing options...
AIM RaJa Posted October 28, 2015 Share Posted October 28, 2015 Nu e optim ce ai facut. Faci multe operatii in plus si astfel pierzi din timpul de executie. int nr = 1; while (nr<=N) { S+=nr * nr; nr++; } Asa iei direct patratele perfecte, iar complexitatea ta e cat se poate de optima (sqrt(N)) Link to comment Share on other sites More sharing options...
ALexu Posted November 19, 2015 Share Posted November 19, 2015 Topic closed ! Link to comment Share on other sites More sharing options...
Recommended Posts