Perl find all pow 2 numbers

This little script finds all pow 2 numbers in Perl :

use strict;
my $number = 0;

for(1..32){
    ++$number;
    print "$number - ".isPow2($number)."\n";
}


sub isPow2 {
    return(0) if($_[0] <= 0);
    return(1) if($_[0] == 1);
    for(1..$_[0]){
       return(1) if($_[0] == 2**$_)
    }
    return(0);
}

Our Result:

1 - 1  
2 - 1  
3 - 0  
4 - 1  
5 - 0  
6 - 0  
7 - 0  
8 - 1  
9 - 0  
10 - 0 
11 - 0 
12 - 0 
13 - 0 
14 - 0 
15 - 0 
16 - 1

Leave a Reply

Your email address will not be published.

15 + two =