Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. # doors accept the number of doors as an argument.
  2. my ($num_doors) = @_;
  3. # All the doors are closed by default.
  4. my @is_open = ((0) x $num_doors);
  5. # Loop over the passes of all the doors.
  6. for my $pass (0 .. ($num_doors-1))
  7. {
  8. my $door = $pass;
  9. # Loop over all the doors in the pass.
  10. while ($door < $num_doors){
  11.  
  12. # Flip the door's state.
  13. $is_open[$door] = !$is_open[$door];
  14. }
  15. continue
  16. {
  17. # Advance the door number.
  18. $door += $pass+1;
  19. }
  20. }
  21. # Print the status of all the doors.
  22. foreach my $door (0 .. $#is_open){
  23.  
  24. printf("Door #%d is %s.n",
  25. $door+1, ($is_open[$door] ? "open" : "closed")
  26. );
  27. }
  28. # Return false so programmers won't depend on our return value.
  29. # See Perl Best Practices .
  30. return;
  31. }
  32. doors(100);
  33.  
  34. 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?:
  35.  
  36. my @doors=1..100;
  37. my $count_open=0;
  38. my $count_closed=100;
  39.  
  40. for (my $i=2; $i <=101; $i++){
  41. for my $nums(@doors){
  42. if($nums % $i==0){
  43. my $prev_vals= $nums-1;
  44. if($nums=$prev_vals){
  45. $count_open++;
  46. $count_closed--;
  47. }
  48. }
  49. }
  50. }
  51. print "$count_openn";
  52. print "$count_closedn";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement