Find the answer to your Linux question: Entire Site Articles Downloads Forums Linux Hosting

# Thread: Calculate Pi to 10,000 digits, then convert that number to binary form

Enjoy an ad free experience by logging in. Not a member yet? Register.
1. ## Calculate Pi to 10,000 digits, then convert that number to binary form

I have to create this program using perl. I was also wondering if I could also find a way to have an input as well to I could calculate more than just (Pi). I have an idea of how to do this, but am confused on how to fully write this out in perl format. I was wondering if anyone knew how to do this, since it would be a huge help. Thanks!

2. I don't code anymore, but years back I had coded any input number, and the output would be in any base. It didn't make any difference if the output had to be binary, base 5, base 7, or octal, or hex it would do all of them.

It used the analogy of two analog wheels, which one rotated counter-clockwise, and the other clockwise. It worked real nice, took very little calculations, and was quite accurate. Sorry, I can't give you any advise on using Perl, but perhaps you could use the same principle as the wheels for your project?

Originally Posted by andrewrckt
I have to create this program using perl. I was also wondering if I could also find a way to have an input as well to I could calculate more than just (Pi). I have an idea of how to do this, but am confused on how to fully write this out in perl format. I was wondering if anyone knew how to do this, since it would be a huge help. Thanks!

3. Answer 1, quick answer: use the @argv array to capture your command line parameters, one parameter with a value for example, another telling your program what you want it to do, e.g.

...
\$what_to_do=\$ARGV[0];
\$value=\$ARGV[1];
...

Answer 2, better answer: use the getopts() function, e.g. refer to www devdaily com/perl/perl-getopts-command-line-options-flags-in-perl (replace the spaces with .)

Answer 3, best Answer: use Answer 2 then buy 'Learning Perl' and 'Programming Perl', both published by O'Reily

Hope this helps,
Chas

Originally Posted by andrewrckt
I have to create this program using perl. I was also wondering if I could also find a way to have an input as well to I could calculate more than just (Pi). I have an idea of how to do this, but am confused on how to fully write this out in perl format. I was wondering if anyone knew how to do this, since it would be a huge help. Thanks!

4. \$spacer_open \$spacer_close
5. ## Are you asking us to do your homework assignment for you?

If so, that's specifically against forum rules.

If not (or even if so), I am interested in your results.

I can program okay in Perl (I recommend O'Reilly's "Programming Perl"), yet I have no idea how one calculates the value of pi...

6. ... ahh, pi, it must be one of those horrible Computer Science assignments...

7. Hi andrewrckt

What would you like us to do? I think, nobody will write the script for you. If you have an idea about how to do it, then start the editor and do it!

Or is it about how to implement the input? Use command line parameters it's propably the most easiest way.

It's propably a help to you to first implement the pi algo and then add additional functuallity to the script. So you have a base that you can extend.

Br
Haze

8. Try looing at the bignum module, setting the precision and select an algorithm to calculate pi.

9. this is not homework. I am doing this for pure curiosity. I have created a small program to take an input of a pi file of one million digits to convert to binary, but it will only go out to 2**64 so i can not convert pi efficiently.

use strict;
use Bit::Vector;

sub frac_bin {
my \$frac = shift;
my \$i = int( \$frac * 2 ** 256 );
print "input: \$frac\n";
my \$vec = Bit::Vector->new_Dec( 256, \$i );
my \$result = \$vec->to_Bin;
print "binary fraction: \$result\n";
return \$result;
}

my \$bits;
\$bits = frac_bin ( 0.141592653589793238462643383279502884197169399375 10582097494459230781640628620899862803482534211706 79 );
\$bits = frac_bin ( 0.75 );
\$bits = frac_bin ( 1 / 3 );

I do not ask for people to write something, but direction. I would love to figure out this question I have.

10. Also to add...i graduated last year with a Meteorology degree, thus I am no longer in that program. I would appreciate some help with this if you guys can.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•