How does this code calculate pi with high precision? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
long f[2801];
int main()
{
long i = 0, c = 2800, d = 0, e = 0, g = 0;
for (i = 0; i < c; ++i)
f[i] = 2000;
for (;;) {
d = 0;
g = c * 2;
if (!g)
break;
i = c;
for(;;) {
d += f[i] * 10000;
--g;
f[i] = d % g;
d /= g;
--g;
--i;
if (!i) break;
d *= i;
}
printf("%.4ld",e+d/10000);
e = d % 10000;
c -= 14;
}
return 0;
}
My question is: How does this code calculate pi with high decimal precision and what is the math formula it uses?

This is a formatted copy of the PI program written by Dik T. Winter of the CWI institute of Holland. Originally written in an obfuscated form, in two or three lines, there are several variations by Dik and other that output different numbers of places of PI (e.g. 800, 15,000, etc.) based on evaluation of a mathematical series.
It is a class of programs known as 'spigot algorithms', designed to output a specific number of digits. You can find out more via a Google search on Dik Winter and 'spigot algorithms'. Some example hits:
Computing Pi in C a detailed analysis of the algorithm with unanswered questions.
Pi the Number, not the Movie

Related

C - what is the problem in the Return part of recursion in this code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
/* Recursive v. Iterative Fibonacci
* simple recursion
* Sally Coder Jan 18, 2018
F(n) = F(n-1) + F(n — 2); F(0) = 0, F(1) = 1 */
#include <stdio.h>
long fibonacci(int n) {
long f2 = 0, f1 = 1, f_old;
int i;
for (i = 0; i < n; i++) {
f_old = f2;
f2 = f2 + f1;
f1 = f_old;
}
return f2;
}
long recursive_fibonacci(int n) {
if (n <= 1)
return n;
else
return (recursive_fibonacci(n — 1) + recursive_fibonacci(n — 2));
}
int main(void) {
int how_many = 0, i;
printf("I want table of fibonacci up to n:");
scanf("%d", &how_many);
printf("\n fibonacci\n");
for (i = 0; i < how_many; i++)
printf("\n%d\t %1d %1d\n", i, fibonacci(i), recursive_fibonacci(i));
}
I am getting an error in the return statement of the function recursive_fibonacci() code.
The problem in your code is in the line return (recursive_fibonacci(n — 1) + recursive_fibonacci(n — 2));.
Specifically, the symbols you used — in n — 1 and n — 2 are not subtraction "-"operator.
Just delete the symbols — and retype them properly. This type of problem does occur when you copy code from some online blog.

C code that finds the number pi using leibniz formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am asked to write the C code that finds the number pi using the Leibniz formula.
However, the result should be 3.14 but result turns 3.23. What is the reason for this?
//Calculating the value of PI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
//function main begins program execution
int main( void )
{
float pi = 0;
size_t k,n;
for ( n = 0 , k = 0; n <= 10 , k <= 10; n++ ,k++) {
pi += ( pow( -1, n ) * 4 )/ ( 2 * k + 1 );
}//end for
printf(" pi is %.2f\n",pi );
getch();
return 0;
}//end main
I played with it by increasing your iterations.
At "200" I got 3.15.
Basically "10" isn't even close to enough.

Please explain the following C code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I compile and run the C code shown below, it generates the following:
Input:
#include <stdio.h>
int main()
{
int i, j;
int a, b;
for (j = 0; j <= 4; j+=2)
{
a = j;
b = 0;
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
printf("%d %d\n", a, b);
}
}
Output:
0 0
2 40
4 80
If anyone can tell me why the following input generates the above output, this would be much appreciated.
Hope you understand it with the simple trace table I draw.
This seems to be a basic C example showing arithmetic and printf statements.
It helps if you break down a problem like this into modules:
1) Execute the steps 2a and 2b, with j = 0, 2, 4, sequentially:
for (j = 0; j <= 4; j+=2)
2a) For each index of j, b = b + 2 * j * i (a = j here)
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
2b) printf("%d %d\n", a, b) is just printing out the values of j (since a is assigned the value of j) and b, where the calculations are done in step 2a.
Next time try to give the exact area where you are confused. Explaining something like this over chat is not easy. You have to break it down on your own really.

C how to check and point a '0' in a int number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Imagine we've got an int number = 1040 or int number = 105 in a C program, and we want to know if this number contains a 0 and in which position/s are they. How could we do it?
Examples
1040 -> position 0 and 2.
1000 -> position 0, 1 and 2.
104 -> position 1.
56 -> NO ZEROS.
Thanks!
I would divide by 10 and check the remainder. If remainder is 0, then last position of the number is 0. Then repeat the same step until number is less than 10
#include<iostream>
int main(void)
{
long int k = 6050404;
int iter = 0;
while (k > 10) {
long int r = k % 10;
if( r == 0) {
std::cout << iter << " ";
}
k = k / 10;
iter++;
}
}
I would convert it to a string; finding a character in a string is trivial.
As a rule of thumb, if you are doing maths on something, it's a number; otherwise, it's probably (or should be treated as) a string.
Alternatively, something like:
#include <stdio.h>
int main(void) {
int input=1040;
int digitindex;
for (digitindex=0; input>0; digitindex++) {
if (input%10==0) {
printf("0 in position %i\n",digitindex);
}
input/=10;
}
return 0;
}
This basically reports if the LAST digit is 0, then removes the last digit; repeat until there is nothing left. Minor modifications would be required for negative numbers.
You can play with this at http://ideone.com/oEyD7N

How to swap even and odd digits in a decimal number? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
1: 123456 should be converted to 214365
2: 12345 should be converted to 103254
int32_t conv(int32_t n){
char buff[16], *p = &buff[1];
int i, v = n < 0 ? -n : n;
sprintf(p, "%010d", v);
for(i=0;i<5;++i,++p){
char c;
c = *p;
*p = p[1];
*(++p) = c;
}
buff[0] = (n < 0) ? '-' : ' ';
return atoi(buff);
}
size_t conv(size_t n){
size_t q, r, wk, mul=1, ret = 0;
for(;n;n/=100, mul*=100){
wk = n % 100;
r = wk % 10;
q = wk / 10;
ret += (r * 10 + q)*mul;
}
return ret;
}
,
First to clarify: there's a difference between an integer and the decimal representation of that integer in a string. You want the second.
As H2CO3 points out you can do this on your own using modulo's: decompose your number into the decimal base, do the swaps then recompose the number (only mul/add).
Here's another way:
Create a string from your integer using malloc and sprintf.
Loop through the string and do the swapping of characters
print that string or if you want convert it back to an integer with atoi

Resources