How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Ring programming language

Table of Contents

Problem Statement

Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day. That night the first sailor wakes up and decides to take his first share early so tries to divide the pile of coconuts equally into five piles but finds that there is one coconut left over, so he tosses it to a monkey and then hides "his" one of the five equally sized piles of coconuts and pushes the other four piles together to form a single visible pile of coconuts again and goes to bed. To cut a long story short, each of the sailors in turn gets up once during the night and performs the same actions of dividing the coconut pile into five, finding that one coconut is left over and giving that single remainder coconut to the monkey. In the morning (after the surreptitious and separate action of each of the five sailors during the night), the remaining coconuts are divided into five equal piles for each of the sailors, whereupon it is found that the pile of coconuts divides equally amongst the sailors with no remainder. (Nothing for the monkey in the morning.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Ring programming language

Source code in the ring programming language

# Project : Sailors, coconuts and a monkey problem

scm(5)
scm(6)

func scm(sailors)
        sm1 = sailors-1
        if sm1 = 0
           m = sailors
        else
           for n=sailors to 1000000000 step sailors    
                m = n
                for j=1 to sailors              
                     if m % sm1 != 0     
                        m = 0                     
                        exit
                     ok
                     m = sailors*m/sm1+1   
                next
                if m != 0 
                   exit 
                ok
           next
        ok
        see "Solution with " + sailors + " sailors: " + m + nl
        for i=1 to sailors 
             m = m - 1                                  
             m = m / sailors
             see "Sailor " + i + " takes " + m + " giving 1 to the monkey and leaving " + m*sm1 + nl
             m = m * sm1
        next
        see "In the morning each sailor gets " + m/sailors + " nuts" + nl + nl

  

You may also check:How to resolve the algorithm 100 doors step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the Oforth programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the MUMPS programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Prolog programming language
You may also check:How to resolve the algorithm File input/output step by step in the Ruby programming language