Find the answer to your Linux question:
Results 1 to 9 of 9
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4

    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. #2
    Just Joined!
    Join Date
    Dec 2007
    Posts
    35
    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?

    Quote Originally Posted by andrewrckt View Post
    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. #3
    Just Joined! chasd's Avatar
    Join Date
    Jan 2011
    Location
    Sunderland, UK
    Posts
    3

    Cool

    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

    Quote Originally Posted by andrewrckt View Post
    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. #4
    Just Joined!
    Join Date
    Nov 2006
    Location
    near Berea, Kentucky (in a tipi)
    Posts
    47

    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...

  5. #5
    Just Joined! chasd's Avatar
    Join Date
    Jan 2011
    Location
    Sunderland, UK
    Posts
    3
    ... ahh, pi, it must be one of those horrible Computer Science assignments...

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    16
    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

  7. #7
    Just Joined!
    Join Date
    Aug 2011
    Posts
    51
    Try looing at the bignum module, setting the precision and select an algorithm to calculate pi.

  8. #8
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4
    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.

  9. #9
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4
    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
  •