473,467 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what this program do


What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}
Mar 26 '08 #1
14 1459
Three Headed Monkey wrote:
What does bellow C program calculate?
Nothing useful. Looks like either an entry in the obfuscated C contest,
or a good example of an overly-smart programmer abusing the language.
I tried my best but couldn't figured it out.
Why not run it and see what it produces?

That said, there's no point - you will never willingly write code like
this, and if you find it in a programme you're maintaining you should
rip it out asap.
Mar 26 '08 #2
Three Headed Monkey wrote:
What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}

The output of that is
03140159026503580979032308460264033803270950028804 19071609390937051005820097049404590230078106400628 06200899086208030482053402110706079802140808065103 2802300664
07090384046009550058022301720535094008120848011107 45002804100270019308520110055509640462029408950493 00380196044208810097056605930344061208470564082303 7806780316
05270120019009140564085606920346003408610045043206 64082103390360072600240914012703720458070006600631 05580817048801520092009602820925040901710536043607 8902590036
00010133005300540882004606520138041406950194015101 60094303050727003605750959019503090218061107380193 02610179031005110854080704460237099602740956073501 8805750272
04890122079308180301019409120983036703360244006506 64030806020139049406390522047307190070021709860094 03700277005309210717062903170675023804670481084607 6609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375 ...
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 27 '08 #3
jacob navia wrote:
The output of that is
03140159026503580979032308460264033803270950028804 19071609390937051005820097049404590230078106400628 06200899086208030482053402110706079802140808065103 2802300664
07090384046009550058022301720535094008120848011107 45002804100270019308520110055509640462029408950493 00380196044208810097056605930344061208470564082303 7806780316
05270120019009140564085606920346003408610045043206 64082103390360072600240914012703720458070006600631 05580817048801520092009602820925040901710536043607 8902590036
00010133005300540882004606520138041406950194015101 60094303050727003605750959019503090218061107380193 02610179031005110854080704460237099602740956073501 8805750272
04890122079308180301019409120983036703360244006506 64030806020139049406390522047307190070021709860094 03700277005309210717062903170675023804670481084607 6609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375 ...
Aha! Good catch!

I changed the format specifier and added a final newline:

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
printf("\n");
}

Which prints:

31415926535897932384626433832795028841971693993751 05829749445923078164062862089986280348253421170679 82148086513282306647093844609555822317253594081284 81117452841027019385211055596446229489549338196442 88197566593344612847564823378678316527120190914564 85669234634861454326648213393607262491412737245870 06606315588174881529296282925409171536436789259361 13353548824665213841469519415116094330572736575959 19530921861173819326117931051185480744623799627495 67351885752724891227938183011949129833673362446566 43086021394946395224737197021798694370277539217176 29317675238467481846766940513

:-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 27 '08 #4
Morris Dovey <mrdo...@iedu.comwrote:
jacob navia wrote:
The output of that is
031401590265035...

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...
...
I changed the format specifier and added a final newline:

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)
It needs to be "%.3ld".
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
printf("\n");

}

Which prints:

31415926...58297...
Which is wrong because it should be ...582097...

--
Peter
Mar 27 '08 #5
On Thu, 27 Mar 2008 01:09:11 +0100, jacob navia <ja***@nospam.comwrote:
Three Headed Monkey wrote:
What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}

The output of that is
0314015902650358097903230846026403380327095002880 4190716093909370510058200970494
0459023007810640062806200899086208030482053402110 7060798021408080651032802300664
0709038404600955005802230172053509400812084801110 7450028041002700193085201100555
0964046202940895049300380196044208810097056605930 3440612084705640823037806780316
0527012001900914056408560692034600340861004504320 6640821033903600726002409140127
0372045807000660063105580817048801520092009602820 9250409017105360436078902590036
0001013300530054088200460652013804140695019401510 1600943030507270036057509590195
0309021806110738019302610179031005110854080704460 2370996027409560735018805750272
0489012207930818030101940912098303670336024400650 6640308060201390494063905220473
0719007002170986009403700277005309210717062903170 6750238046704810846076609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375 ...
Thank you Jakob, that is very useful information.

I think error may be author wanted to print space instead of zero in
printf format. Or a should be 10000 and compute 4 digits every loop.

Is this known algorithm? What is name of algorithm? All algorithms in
WWW are very complicated and long, but this algorithm is complicated and
short and I like it. How does it work? Code seems not very clear. If I
use long long, how many digits can it print?

What is limit that can be done in C language?

Mar 27 '08 #6
Peter Nilsson wrote:
Morris Dovey <mrdo...@iedu.comwrote:
>jacob navia wrote:
>>The output of that is 031401590265035...

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...
...
I changed the format specifier and added a final newline:

#include <stdio.h>
main() {
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)

It needs to be "%.3ld".
> for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
printf("\n");
}

Which prints:

31415926...58297...

Which is wrong because it should be ...582097...
Which the corrected version (with a = 10000) does generate. I
haven't the vagues idea of the algorithm.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '08 #7
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>I haven't the vaguest idea of the algorithm.
See http://www.mathpropress.com/stan/bib...phy/spigot.pdf

In short: if you represent pi in the "mixed-radix" base where the
values for each digit overflow at

. 1/3 2/5 3/7 4/9 ...

(that is, the first digit after the point represents thirds, the
second multiples of 1/3 * 2/5, and so on),

then the representation of pi is

2 . 2 2 2 2 ...

and the program works by converting that to base 10 (actually, 10000).

A similar algorithm for calculating the digits of e - also given in
the same paper - is much easier to understand.

-- Richard
--
:wq
Mar 27 '08 #8
In article <ab****************@aioe.orgThree Headed Monkey <fo****************@yahoo.comwrites:
I think error may be author wanted to print space instead of zero in
printf format. Or a should be 10000 and compute 4 digits every loop.
Right. 10000 and %.4d. At least that is what my .plan says.
Is this known algorithm?
Eh, yes. It uses a mixed base representation of pi.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 27 '08 #9
Richard Tobin wrote:
See http://www.mathpropress.com/stan/bib...phy/spigot.pdf
Thanks for the link!

I got to thinking about how one might go about using the pi and e
digit strings to build a random number generator. Seemed like a
good idea at the time. :-)

Then I wrote a tiny program to give me the digit frequencies and
decided that the idea might not be as good as I'd thought it
might be. With a initialized to 10000 and the printf() format
changed to "%.3ld" to produce the digits, I ran my little counter
program to get:

0 56
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

Which surprised me because I'd expected a much more even
distribution.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 27 '08 #10
In article <47***************@iedu.comMorris Dovey <mr*****@iedu.comwrites:
....
I got to thinking about how one might go about using the pi and e
digit strings to build a random number generator. Seemed like a
good idea at the time. :-)

Then I wrote a tiny program to give me the digit frequencies and
decided that the idea might not be as good as I'd thought it
might be. With a initialized to 10000 and the printf() format
changed to "%.3ld" to produce the digits, I ran my little counter
program to get:
....
Which surprised me because I'd expected a much more even
distribution.
You would have gotten that with the format string "%.4d". On the other
hand, using pi and e for random digits only works when they are normal
to base 10 (and there must be some additional randomness in the digits).
It is unknown whether they are.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 27 '08 #11
"Dik T. Winter" wrote:
>
In article <47***************@iedu.comMorris Dovey <mr*****@iedu.comwrites:
Which surprised me because I'd expected a much more even
distribution.

You would have gotten that with the format string "%.4d". On the other
hand, using pi and e for random digits only works when they are normal
to base 10 (and there must be some additional randomness in the digits).
It is unknown whether they are.
I tried that, with this result:

0 74
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

which is more uniform, but less so than I'd expected.

The "digit spigot" is very interesting!

Thanks

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 27 '08 #12
In article <47***************@iedu.com>,
Morris Dovey <mr*****@iedu.comwrote:
>which is more uniform, but less so than I'd expected.
See http://mathworld.wolfram.com/PiDigits.html, which includes a table
of the distribution of digits in the first 10^n places for n up to 12,
and says it "shows no statistically significant departure from a
uniform distribution".

-- Richard
--
:wq
Mar 27 '08 #13
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
I haven't the vaguest idea of the algorithm.

See http://www.mathpropress.com/stan/bib...phy/spigot.pdf

In short: if you represent pi in the "mixed-radix" base where the
values for each digit overflow at

. 1/3 2/5 3/7 4/9 ...

(that is, the first digit after the point represents thirds, the
second multiples of 1/3 * 2/5, and so on),

then the representation of pi is

2 . 2 2 2 2 ...

and the program works by converting that to base 10 (actually, 10000).
Very clever.

Richard
Mar 27 '08 #14
Morris Dovey <mr*****@iedu.comwrites:
Richard Tobin wrote:
>See http://www.mathpropress.com/stan/bib...phy/spigot.pdf

Thanks for the link!

I got to thinking about how one might go about using the pi and e
digit strings to build a random number generator. Seemed like a
good idea at the time. :-)

Then I wrote a tiny program to give me the digit frequencies and
decided that the idea might not be as good as I'd thought it
might be. With a initialized to 10000 and the printf() format
changed to "%.3ld" to produce the digits, I ran my little counter
program to get:

0 56
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

Which surprised me because I'd expected a much more even
distribution.
Looks even enough to me considering the numbers involved.
Mar 27 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
0
by: Nathanael | last post by:
Hi Guys, You probably have heard of affiliate marketing before but what exactly is affiliate marketing? In this introduction you'll find an explanation of the basics of affiliate marketing. The...
13
by: SailFL | last post by:
I have read threads here and there and I have looked at MS but I can not get a clear understanding of what .Net acutally is. I understand that C++ and Vb and other languages are being out a .Net. ...
14
by: Salad | last post by:
On the computer side of the businees there is me, the developer. Another person's role is that of the idea man...the person that knows the business and requirements and issues for the business. ...
9
by: Marek Kurowski | last post by:
Yo! What mean when program is ALPHA or BETA version? I suppose it is not release version of program, but I don't know what it exactly mean. What it mean in your opinion? Marek Kurowski
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
11
by: John Salerno | last post by:
From my brief experience with C#, I learned that it was pretty standard practice to put each class in a separate file. I assume this is a benefit of a compiled language that the files can then be...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.