By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,279 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,279 IT Pros & Developers. It's quick & easy.

Prime Numbers Code - Begginer

P: 4
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. $n = 10;
  4. @$A[300] = "Initialize..";
  5.  
  6. for($p=2;$p<$n;$p++){
  7.     @$A[$p] = $p;
  8. }
  9.  
  10. for($p=2;$p < sqrt($n);$p++){
  11.  if(@$A[$p] != 0){
  12.     $j = $p * $p;
  13.     while($j <= $n){
  14.         @$A[$j] = 0;
  15.         $j = $j + $p;
  16.         print $j,"\t";
  17.     }
  18.  }
  19. }
  20.  
Feb 20 '08 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. $n = 10;
  4. @$A[300] = "Initialize..";
  5.  
  6. for($p=2;$p<$n;$p++){
  7.     @$A[$p] = $p;
  8. }
  9.  
  10. for($p=2;$p < sqrt($n);$p++){
  11.  if(@$A[$p] != 0){
  12.     $j = $p * $p;
  13.     while($j <= $n){
  14.         @$A[$j] = 0;
  15.         $j = $j + $p;
  16.         print $j,"\t";
  17.     }
  18.  }
  19. }
  20.  
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
Feb 20 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. $n = 10;
  4. @$A[300] = "Initialize..";
  5.  
  6. for($p=2;$p<$n;$p++){
  7.     @$A[$p] = $p;
  8. }
  9.  
  10. for($p=2;$p < sqrt($n);$p++){
  11.  if(@$A[$p] != 0){
  12.     $j = $p * $p;
  13.     while($j <= $n){
  14.         @$A[$j] = 0;
  15.         $j = $j + $p;
  16.         print $j,"\t";
  17.     }
  18.  }
  19. }
  20.  

What is the purpose of this line?

@$A[300] = "Initialize..";
Feb 20 '08 #3

P: 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 :)
Feb 20 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
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?
Feb 20 '08 #5

P: 4
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.
Feb 22 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
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.
Feb 22 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.