Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:19:21+00:00 2026-05-26T17:19:21+00:00

5 monkey share n peaches, they cannot distribute equally. So the first monkey dump

  • 0

5 monkey share n peaches, they cannot distribute equally. So the first monkey dump 1 peach, and total number of peaches can be divided by 5, and the first monkey took his part.

Then is the second monkey, -1 peach, can be divided by 5 and took his part.
Until the fifth monkey finished all the steps. There may be some peaches still left.

Give the minimum number of peaches that satisfy this condition.

perl code 1:

#!/usr/bin/perl -w
for $n (0..10000){      #this is basic idea but code is too messy !
    if( ($n-1) % 5 == 0 ){
     $remain = 4/5 * ($n -1 );
         if( ($remain - 1) % 5 == 0){
           $remain = 4/5 * ($remain -1 );
           if( ($remain - 1) % 5 == 0){
               $remain = 4/5 * ($remain -1 );
               if( ($remain - 1) % 5 == 0){
                   $remain = 4/5 * ($remain -1 );
                   if( ($remain - 1) % 5 == 0){
                      $remain = 4/5 * ($remain -1 );
                      print "remain: $remain original: $n\n";
                   }
               }
            }
          }
     }
 }

perl code 2:

sub doit($){
    ($n) = @_;
    if( ($n - 1) % 5 ==0 ){ #if can be distributed by 5 monkey
       $n = ($n - 1) * 4/5;  #commit distribute
       return $n;
    }else{
       return -1;  #fail
    }
}

for $n (0..10000){   #restriction
    $r = $n;    #"recursively" find solution
    $o = $n;    #backup n
    $count = 0;
    for ($i = 0; $i < 5; $i++){  #assume there is 5 monkey, it can be changed
       $r = doit($r);
    if($r == -1){   #skip once fail
        last;
    }
    $count++;
    }
    if($count == 5){ # if pass 5 test, then you found the number !
       print "now ".$r."\n";
       print "origin ".$o."\n";
    }
}

I am thinking to cut some code. But felt hard. Can anyone help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:19:22+00:00Added an answer on May 26, 2026 at 5:19 pm

    First of all, you really should use strict and warnings pragmas at the top of your scripts. Your $n usage is especially worrisome. In the future, if you declare variables with my but use the same name, you convey the fact that they will represent the same quantity, without the fear that they might collide.

    Anyway here is a slightly polished, and more importantly strict and warnings safe version:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    sub doit {
        my ($n) = @_;
        if( ($n - 1) % 5 ==0 ){ #if can be distributed by 5 monkey
           $n = ($n - 1) * 4/5;  #commit distribute
           return $n;
        } else {
           return undef;  #fail
        }
    }
    
    OUTER: for my $n (0..10000){   #restriction
        my $r = $n;    #"recursively" find solution
        for (1..5){  #assume there is 5 monkey, it can be changed
           $r = doit($r);
           next OUTER unless defined $r;
        }
        # if code gets here, then it passed 5 test, then you found the number !
        print "now: $r\torigin: $n\n";
    }
    

    And now, if you really want to be fun with it (don’t use this in production, readability first! ):

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    OUTER: for my $n (0..10000){ 
        my $r = $n;
        $r = ($r - 1) % 5 ? next OUTER : 4/5 * ($r - 1) for (1..5);
        print "now: $r\torigin: $n\n";
    }
    

    or even golfed:

    for(0..10000){$r=$n=$_;map$r*=--$r%5?next:4/5,1..5;print"now: $r\torigin: $n\n"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import random wordlist = {'Candy', 'Monkey'} level = 0 while level == 0: number
I'm monkey patching a package using a technique given at the beginning of How
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,
Here is one method that monkey patched the Dir[] method from autotest class Dir
I need help getting a Grease Monkey with JQuery Script to run on a
I'd really like to handle this without monkey-patching but I haven't been able to
I've recently started working with Aptana and Eclipse Monkey. What I want to do
According to Wikipedia, a monkey patch is: a way to extend or modify the
One of Steve McConnell's checklist items is that you should not monkey with the
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.