Prime Numbers Code - Begginer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miko
    New Member
    • Apr 2006
    • 4

    Prime Numbers Code - Begginer

    Hello,
    I am trying to get this code to print all prime numbers, this code was psudo code and i am trying to translate it into perl, but it seems that i have some technical difficulties as this is my first program on perl

    OS: Windows Xp / Intel x86
    Error: Wierd Numbers coming out!
    Comment: This is not my an H.W

    Code:
    #!/usr/bin/perl -w
    
    $n = 10;
    @$A[300] = "Initialize..";
    
    for($p=2;$p<$n;$p++){
        @$A[$p] = $p;
    }
    
    for($p=2;$p < sqrt($n);$p++){
     if(@$A[$p] != 0){
        $j = $p * $p;
        while($j <= $n){
            @$A[$j] = 0;
            $j = $j + $p;
            print $j,"\t";
        }
     }
    }
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by miko
    Hello,
    I am trying to get this code to print all prime numbers, this code was psudo code and i am trying to translate it into perl, but it seems that i have some technical difficulties as this is my first program on perl

    OS: Windows Xp / Intel x86
    Error: Wierd Numbers coming out!
    Comment: This is not my an H.W

    Code:
    #!/usr/bin/perl -w
    
    $n = 10;
    @$A[300] = "Initialize..";
    
    for($p=2;$p<$n;$p++){
        @$A[$p] = $p;
    }
    
    for($p=2;$p < sqrt($n);$p++){
     if(@$A[$p] != 0){
        $j = $p * $p;
        while($j <= $n){
            @$A[$j] = 0;
            $j = $j + $p;
            print $j,"\t";
        }
     }
    }
    Ok, you have shared your code and said you had a problem. can you please share with us what problem you are having and give us the error(s)?

    Also, please know that you should be using the pragmas "use strict;" and "use warnings;" in all of your programs. Most people won't look at your code unless you do.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by miko
      Hello,
      I am trying to get this code to print all prime numbers, this code was psudo code and i am trying to translate it into perl, but it seems that i have some technical difficulties as this is my first program on perl

      OS: Windows Xp / Intel x86
      Error: Wierd Numbers coming out!
      Comment: This is not my an H.W

      Code:
      #!/usr/bin/perl -w
      
      $n = 10;
      @$A[300] = "Initialize..";
      
      for($p=2;$p<$n;$p++){
          @$A[$p] = $p;
      }
      
      for($p=2;$p < sqrt($n);$p++){
       if(@$A[$p] != 0){
          $j = $p * $p;
          while($j <= $n){
              @$A[$j] = 0;
              $j = $j + $p;
              print $j,"\t";
          }
       }
      }

      What is the purpose of this line?

      @$A[300] = "Initialize ..";

      Comment

      • miko
        New Member
        • Apr 2006
        • 4

        #4
        Problem, i am new to perl, debugging code for me isn't as easy as C or Java

        I use initialize because IDE gives wierd
        warning: Useless use of array slice in void context

        I have no idea what this means??!!

        Sry if my explaination ins't clear enough, and i am ready to answer any question, thx for all of ya for help :)

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by miko
          Problem, i am new to perl, debugging code for me isn't as easy as C or Java

          I use initialize because IDE gives wierd
          warning: Useless use of array slice in void context

          I have no idea what this means??!!

          Sry if my explaination ins't clear enough, and i am ready to answer any question, thx for all of ya for help :)
          If you were that new to perl I doubt you could have written that code. Is this a school/class assignment of some kind?

          Comment

          • miko
            New Member
            • Apr 2006
            • 4

            #6
            Originally posted by KevinADC
            If you were that new to perl I doubt you could have written that code. Is this a school/class assignment of some kind?
            No, i am just doing work that i am not asked for, i did get this code to work with C but i can't get it to work with perl.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by miko
              No, i am just doing work that i am not asked for, i did get this code to work with C but i can't get it to work with perl.
              Is your purpose to learn perl or just figure out this one problem? If it is to learn perl you need to start at the beginning with the basic data types. If it is to figure out this one problem just google for something like :

              perl prime numbers


              and I am sure you will find many solutions.

              Comment

              Working...