Printing prime numbers from 1 through 100

前端 未结 22 2507
无人共我
无人共我 2020-11-28 05:14

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.

But I don\'t think tha

22条回答
  •  孤独总比滥情好
    2020-11-28 05:55

    I did it in perl based on the most popular answer by ProdigySim's 2nd method. I had to add a break equivalent in perl, last, right after the print $i . " \n"; to avoid outputting the primes twice.

    #!/bin/perl
    use strict;
    
    for(my $i=2; $i < 100; $i++){
    
        my $prime = 1;
    
        for (my $j=2; $j*$j<=$i; $j++){
            if ($i % $j == 0){
                $prime = 0;
                last;
            }
            if($prime) {
                print $i . " \n";
                last;
            }
        }
    
    }
    

提交回复
热议问题