@COCONUT BASdCOCONUT DOCCOCONUT HBO COCONUT HBS&"COCONUT MAC-ݙCOCONUT MSEKyCOCONUT PASM $COCONUT TINWrCOCONUTMCOM^COCONUTPCOM`A3 100 T(5)=0 110 T(0)=2.2 120 WHILE T(0) <> INT(T(0)) 130 T(5)=T(5)+5 140 FOR Q=4 TO 0 STEP -1 150 T(Q)=5/4*T(Q+1)+1 160 NEXT Q 170 WEND 180 PRINT "TOTAL COCONUTS=";T(0) 190 END 40 FOR Q=4 TO 0 STEP -1 150 T(Q)=5/4*T(Q+1 PROGRAMMING CONTEST #1 ( Reference CTKUG Newsletter Vol. 5 ) PROBLEM: Five missionaries came upon a pile of coconuts on their way home from work. They decided to split up the pile the next morning. They turned in for the night. At one in the morning, one of the missionaries got up, went out, and took a look at the pile of coconuts. He split it into five equal piles, chucked the single extra coconut which was left over to a monkey he saw in a tree close by and put his fifth in the trunk of his car. He figured that the others would never miss what he took. One by one, the other four missionaries got up, split the pile left by his predecessor into 5 equal piles, took his fifth and chucked the single coconut left over to the monkey. In the morning, the remaining pile was split up into exactly five equal piles. The monkey didn't get anything. How many coconuts were there in the beginning for all this to be possible? SOLUTION: Let's start by stating that there were 'n' coconuts discovered by the missionaries . The first missionary, named M1, illicitly took his fraction and left the remaining pile with 'x1' coconuts. If we express this algebraically: M1: 4/5 * (n - 1) = x1 OR n = (5/4 * x1) + 1 This is repeated by missionaries named M2, M3, M4, and M5. Again, this can be expressed algebraically: M2: 4/5 * (x1 - 1) = x2 OR x1 = (5/4 * x2) + 1 M3: 4/5 * (x2 - 1) = x3 OR x2 = (5/4 * x3) + 1 M4: 4/5 * (x3 - 1) = x4 OR x3 = (5/4 * x4) + 1 M5: 4/5 * (x4 - 1) = x5 OR x4 = (5/4 * x5) + 1 Finally, we were told that ultimately there were no coconuts to share with the monkey. In other words, x5 when divided by 5 is a whole number or integer. Algebraically, x5 / 5 = integer OR x5 = 5 * integer Notice that there are 7 variables (n, x1, x2, x3, x4, x5, integer) and only 6 equations. Therefore, there is no direct solution to the problem. The best that we can do is test a series of values against the 6 'criteria' until we find a value that satisfies all the equations. We know that x5, as a minimum, is equal to 5, or integer = 1. This is as good as any place to start. It is obvious that x5 = 5 will not work, so try integer = 2,3,4... corresponding to x5 = 10,15,20,... At long last we will find a value of x5 which satisfies all. We can then solve directly for 'n'. I wrote my program entry in Borland's Turbo Pascal. The 23 lines compile to 12k. This is surprisingly large!! It problably has to do with my use of some 'built-in' functions. I was happily surprised by its execution time of less than 3 seconds. Jim Thompson, 14 Mar 86, 654 W Main St., New Britain, CT, 06053 ly sur00,+138 L ON COCONUT.HBS 3/18/86 01,+233 ST N N=1 02,+133 A L N N=N+5 03,+339 A FV 04,+233 ST N 05,+141 L ZR C=0 06,+212 ST C 07,+133 L N M=N 08,+235 ST M 09,+135 B L M O=M-1 10,+438 S ON 11,+237 ST O 12,+137 C L O S=O/5 13,+639 D FV 14,+236 ST S 15,+539 M FV IF S*5<>O GO TO A 16,+437 S O 17,-702 BRNZ A 18,+136 L S M=4*S 19,+540 M FR 20,+235 ST M 21,+112 L C C=C+1 22,+338 A ON 23,+212 ST C 24,+439 S FV 25,-409 BRN B IF C<5 GO TO B 26,-230 BRP D IF C=6 GO TO D 27,+135 L M O=M 28,+237 ST O 29,-112 BR C GO TO C 30,+133 D L N PRINT N 31,+800 PT 32,+900 STOP HALT 33,+000 N DS 01 VARIABLE N 34,+000 C DS 01 C 35,+000 M DS 01 M 36,+000 S DS 01 S 37,+000 O DS 01 O 38,+001 ON DW +001 CONSTANT 1 39,+005 FV DW +005 5 40,+004 FR DW +004 4 41,+000 ZR DW +000 O  L ON COCONUT.HBS 3/18/86 ST N N=1 A L N N=N+5 A FV ST N L ZR C=0 ST C L N M=N ST M B L M O=M-1 S ON ST O C L O S=O/5 D FV ST S M FV IF S*5<>O GO TO A S O BRNZ A L S M=4*S M FR ST M L C C=C+1 A ON ST C S FV BRN B IF C<5 GO TO B BRP D IF C=6 GO TO D L M O=M ST O BR C GO TO C D L N PRINT N PT STOP HALT N DS 01 VARIABLE N C DS 01 C M DS 01 M S DS 01 S O DS 01 O ON DW +001 CONSTANT 1 FV DW +005 5 FR DW +004 4 ZR DW +000 O END  O ON DW +001 CONSTANT 1; ; COCONUT.MAC 3/18/86 ; .Z80 ASEG ORG 100H LD HL,1 ;N1=1 LD (N1),HL A1: LD HL,(N1) ;N1=N1+5 LD DE,5 ADD HL,DE LD (N1),HL LD HL,0 ;CT=0 LD (CT),HL LD HL,(N1) ;M1=N1 LD (M1),HL B1: LD HL,(M1) ;O1=M1-1 DEC HL LD (O1),HL C1: LD HL,(O1) ;S1=O1 div 5 LD DE,5 CALL DIV LD (S1),HL ;If S1*5 <> O1 LD HL,(S1) LD DE,5 CALL MULT LD DE,(O1) SBC HL,DE JR NZ,A1 ;then go to A1 LD HL,(S1) ;else LD DE,4 ;M1=4*S1 CALL MULT LD (M1),HL LD HL,(CT) ;CT=CT+1 INC HL LD (CT),HL LD DE,5 ;If CT<5 SBC HL,DE JR C,B1 ;then go to B1 JR NZ,D1 ;if CT=6 then go to D1 LD HL,(M1) ;else O1=M1 LD (O1),HL JR C1 ;go to C1 D1: LD HL,(N1) ;Print N1 CALL DISP CALL 0 ;Return ; ; Divide subroutine ; HL=HL DIV DE ; DIV: CALL DIVMOD LD H,B LD L,C RET ; ; This does the work for 'divide' and 'modulus' ; BC=HL div DE; HL=HL mod DE ; DIVMOD: XOR A ;A=0 EX DE,HL DM2: BIT 6,H ;Normalize divisor JR NZ,DM3 INC A ADD HL,HL ;Shift left JR DM2 DM3: EX DE,HL LD BC,0 ;BC=0 (result register) INC A DM4: OR A ;Clear flags SBC HL,DE ;Subtract divisor CCF JR C,DM5 ADD HL,DE ;Result is negative OR A DM5: RL C ;Shift 0 or 1 into quotient RL B SRA D ;Shift divisor RR E DEC A ;Count bits JR NZ,DM4 RET ; ; Multiply subroutine ; HL=HL*DE ; MULT: LD B,H ;BC=HL LD C,L LD HL,0 ;HL=0 (result register) MY1: SRA B ;BC=BC/2 RR C ;LSB to carry JR NC,MY2 ADD HL,DE ;Add multiplicand if bit set MY2: LD A,B OR C RET Z ;Finished if BC=0 SLA E ;DE=2*DE RL D JR MY1 RET ; ; Display subroutine ; Display HL as a decimal string ; DISP: LD D,1 PUSH DE ;Set last digit flag DS2: LD DE,10 CALL DIVMOD ;BC=HL/10 HL=HL\10 PUSH HL LD H,B ;Restore quotient LD L,C LD A,H OR L JR NZ,DS2 ;Loop til quotient is 0 DS3: POP DE ;Restore digit LD A,D OR A RET NZ ;Exit when flag is found LD A,E ADD A,'0' ;Convert digit to ASCII LD E,A LD C,2 ;Display it with ConOut function CALL 5 JR DS3 N1: DW 0 CT: DW 0 M1: DW 0 S1: DW 0 O1: DW 0 END ~ COCONUT.MSE 2/27/85 1 v : 1 n : (v. ^ n. 5 + n : n. m : #F,m;[ #F,m;[ #F,m;[ #F,m;[ #F,m;[ m. 5 \ 0 = [0 v : ]]]]]] ) n. ! $F ~ evaluate (1%. - 1)/5*4 1%. 1 - 5 \ 0 = [1%. 1 - 5 / 4 * 1% : 1 @] 0 @ VAR i : byte ; (* Counter for number of missionaries *) j : integer ; (* Counter for 'Morning coconuts' *) x, y : real ; (* No. of Nuts *) BEGIN i := 1 ; (* Initialize counter *) j := 1 ; (* Initialize counter *) y := 5 ; (* Minimum no. of 'Morning coconuts' *) WHILE i < 6 DO (* 'i' = No. of Missionaries + 1 *) BEGIN x := 1.25 * y + 1 ; (* Calc. # nuts each missionary divided *) IF Frac(x) > 0 THEN (* Check that each pile has 'whole' nuts *) BEGIN j := j + 1 ; (* Try the next whole no. of coconuts *) y := 5 * j ; (* Morning coconuts MUST be a 5 multiple *) i := 0 ; (* Re-initialize missionary counter *) END ELSE y := x ; (* Pass whole # nuts to next missionary *) i := i + 1 ; (* Try next missionary's division *) END; WRITELN (CON,'There were a minimum of', x:5:0,' coconuts in the original pile !!'); END. :"COCONUT.TIN, 3/18/86." `Announce program.' n=1 `Initialize coconut count.' A n=n+5 `Bump by 5.' c=0 `Reset counter.' m=n `Work with m.' B o=m-1 `Decrease by 1.' C s=o/5 `Check if divisible by 5.' {o<>s*5}->A `Start again if not' m=4*s `else update work variable' c=c+1 `and counter.' {c<5}->B `Do for all 5 missionaries.' {c==6}->D `Go and print answer if c is 6.' o=m `Each missionary has some...' ->C `Need one last check.' D :"The answer is " `Print the ' ;n `answer and ' ! `halt.'  :"The answer!"*"!"*"*+"*m"*͖[R *͖"*#"R8 *"*ͬs`iɯt <)28!?"9!!>2 :D]SXN]D [ (!e}̈́A8Q0G: x@!\w# (   yV. V!h6# (*(.(!8}(*(̈́w#>?> w#a{ |͒}͛Ɛ'@'7||}>"C"6# ""͐ͩ*B"[R5*"^#V#^#V#N#FO/o&9O/o&9!9(> (G!9 w#E͊w}8uRB0 >R@RR!+ͨ z R!+ͨ z <!+ͨ z <!+ͨ z <!#ͨ z <!+ͨ z T]KB!z> S>))0 = |JJDMgo>jB0 7?= H\<z5+)+<z {0Gɯgo||H}||/g}/o#}o&K[xAJSJDM!b"!6J"DM'ͬͬdͬ ͬ} wͦWͧ _}8(8J`9{T]=o`9y w >uJ u` }>(; xQ }} ˸T}ٕ(0D=C ,= ( [ 0%D , 7 ͏ ?(8u x O - ; 8˸x X ,-xG}; }م 9; .>#n0[ D = - nx P ,-(-˸G,-; }ٕ? 9.>͏ 8u ?= u+-(>O 0u O 8͏ ?x P , 78ƀ8ƀ8ox٨!دoGOW_gɷɷ|لg{ً_zيWyىOxوG|ٔg{ٛ_zٚWyٙOx٘Gxٸyٹzٺ{ٻ|ټx٨ xx(ͼ ?}ٽÏ }ց; <(; 7D = |٤g{٣_z٢Wy١Ox٠GD u J }x>uu}ƀ/ƀo; -J }0W-J W,}l˸ͨ 8 ; ` x( -ͨ 8J -ͨ 8,J }l8;*!` ! >u` ` u--- J ,,,-xGg?+2n*8t z~,->uxua}.; OJ , ; !U >,k- o&0%,` }g; }؉}颋.:}8c~I$I~L*kٷx˸; }0G,͙<},-(-J ! >0 a` o8 Oþ >um.`1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N#FB ͟r+s> !T]>)j)0 0= UR!#U*^#V#N#F#^#V>">!2DM"~x(L* :O(o:" C}=( ?*-N#Fp+qq#p! * F+N+++V+^Bq#p>>> SRѷR* s#r$ s#r"S"! N#FB(^x * 6#[<(H*! Kq#p##K[! *! 4 #4! x *$ *>w""{_!"*nf}(HR0nf" ^VMDnfutqp*s#r*s#r"* 5KB!>u~#fo{_"*R0RnfR0KqputsrNF( ^VNF^V*SutKqp R*R(~w~wnf ut"6# * *!""*NFy(* "*B0Cnf* [R*"*RS[s#r^#VS>O"w2x2!"" @*>2"!"""!\Ͳ*: !~6go(\R*s#r_2x( s x(T]DMR0 -a%}̈́o*!~6o&͠|ͣ}%^C User break1:% I/O% Run-time% error ͒%, PC=[R"͍% Program aborted*1!͍! !Ͳ!}2!"!!͡*&!ͯEʨ ! ! !ͳ !͡! !ͥEʉ *!"!*!͡!}2Ö !!͡*&!}2![́There were a minimum of!!!@́! coconuts in the original pile !!͐b!͡*&!}2![́There were a minimum of!!!@́! coconuts in the original pile !