Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # doors accept the number of doors as an argument.
- my ($num_doors) = @_;
- # All the doors are closed by default.
- my @is_open = ((0) x $num_doors);
- # Loop over the passes of all the doors.
- for my $pass (0 .. ($num_doors-1))
- {
- my $door = $pass;
- # Loop over all the doors in the pass.
- while ($door < $num_doors){
- # Flip the door's state.
- $is_open[$door] = !$is_open[$door];
- }
- continue
- {
- # Advance the door number.
- $door += $pass+1;
- }
- }
- # Print the status of all the doors.
- foreach my $door (0 .. $#is_open){
- printf("Door #%d is %s.n",
- $door+1, ($is_open[$door] ? "open" : "closed")
- );
- }
- # Return false so programmers won't depend on our return value.
- # See Perl Best Practices .
- return;
- }
- doors(100);
- This is my version, which I am currently working on. Is it possible to continue the code based on the way I am doing it?:
- my @doors=1..100;
- my $count_open=0;
- my $count_closed=100;
- for (my $i=2; $i <=101; $i++){
- for my $nums(@doors){
- if($nums % $i==0){
- my $prev_vals= $nums-1;
- if($nums=$prev_vals){
- $count_open++;
- $count_closed--;
- }
- }
- }
- }
- print "$count_openn";
- print "$count_closedn";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement