can someone help me with this code it must be in c++
it's say" write a code that do this:"
pic related
>>57854788
explain that means ty
>>57854788
>>57854788
You want the sum of every term from 1/1 to 1/1000^2.
Think about it, what does iterate through a finite numbers of elements.
>>57854788
No. I'll give you the answer in python insteadprint(sum(1 / i ** 2 for i in range(1, 10001)))
>>57854788
>preface: I never wrote a C++ program in my life
>this is my attempt in 2 minutes, with 10 seconds of Googling#include<cmath>
#include<iostream>
int main ()
{
int sum = 0;
for (int i=0; i<10000; i++){
sum += 1 / pow(i,2);
}
cout << sum << endl;
}
It probably has a mistake but you can figure out a fix?
>>57854788
You only need one loop of your choice.
>>57854788double sum = 0;
for(int i=1;i<10000;i++){
sum+=1/(i*i);
}
cout << "Wilno nashe: "<< sum << endl;
#include <cmath>
#include <iostream>
using namespace std;
int main(void)
{
cout << pow(4.0*atan(1.0), 2)/6.0 << endl;
return 0;
}
Near enough.
>>57855152
Fuck you
>>57854788
dumb polak
>>57854788#include <boost/math/special_functions/zeta.hpp>
#include <iostream>
int main() {
std::cout << boost::math::zeta<double>(2) << std::endl;
}
Your welcome ;^)
>>57855123
Start with i=1 because you are dividing by it so you will hit a division by zero,
end with i<=1000 because it has to take into account the last element, if not it will go to 999 only.
>>57855170
>Integer division
Pretty sure OP's still the one getting fucked
>>57855123
It uses an int for the sum so all of the terms except for 1/1 will round to 0. It also divides by 0 on the first iteration
Challenge: Make OP's code in the least efficient manner.int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
for(int i = 0; i < 10000, i++)
{
for(int j = 0; j < i; j++)
{
sum1++;
}
for(int k = 0; k < sum1; k++
{
sum2 += sum1;
}
sum3 += 1 / sum2;
}
cout << sum3;
Why people sum rational numbers to integers?
Fucking neo /g.
>>57855195
you're advanced nig/g/a
>>57855262
Challenge accepted.
This code is actually valid, it will just take forever to run:int main()
{
auto inc = [](auto n) {
return -~n;
};
auto mul = [=](auto a, auto b) {
auto x = 0;
while (a != 0) {
for (int i=0; i<b; i = inc(i)) {
x = inc(x);
}
a += a - inc(a);
}
return x;
};
double result;
for (int i=1; i<10000; i=inc(i)) {
result += 1./(mul(i, i));
}
cout << result << endl;
return 0;
}