A computer science problem by Spock Weakhypercharge

//What value will this print out, rounded to the nearest hundredths?
void main ()
{
 int x=1;
 float kittens=0;
 while (x<600000000000)
 {
  if (x%2==1)
  {
   kittens=kittens+(1.0/(2*x-1));
  }
  else 
  {
   kittens=kittens-(1.0/(2*x-1));
  }
  x++;
 }
  kittens=4*kittens;
    printf("%f\n",kittens);
}


The answer is 3.14.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Saumitra Paira
Mar 8, 2014

x is increasing by 1 each time, correspondingly values of kittens are operated as=1-1/3+1/5-1/7+--------------------=tan(inverse)(1)=pi/4 now kittens=4*pi/4; therefore kittens=pi=3.143--- thanks

I pretty much built this around that one formula: http://mathworld.wolfram.com/PiFormulas.html

Frankly there are tonnes of these identities and they are worth glancing over. It's funny how simple rules run complex things.

Sanjay Kamath
Apr 6, 2014

No need to run the problem for the full number of iterations. Here an approximation does just fine.

Sandeep Thakur
Mar 17, 2014

I ran this in JS, but with smaller number.. I got result 3.141605... I changed numbers, but got same result.. So thought that It will be same for that number as well.

Nearest hundredth is: 3.14 therefore...

JS is very nice, no? I feel like the only person who doesn't like Python very much, the lack of semicolons make it feel naked and the functions and loops aren't like any other language I know. This problem was published on Pi Day.

Spock Weakhypercharge - 7 years, 1 month ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...