All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers
1+5+6+7 = 21? (Posted on 2009-03-12) Difficulty: 3 of 5
Use the numbers 1, 5, 6 and 7 exactly once each, combined with as many additions, subtractions, multiplications and divisions as necessary to result in 21.

There are no tricks. Base 10 is used; digits are not combined into integers of more than one digit; no factorial or exponentiation is used. There are no square roots or modular arithmetic.

Parentheses are allowed so as to let you specify the order of the allowed operations that you select.

  Submitted by Charlie    
No Rating
Solution: (Hide)
DECLARE SUB addOn ()
DEFDBL A-Z
CLEAR , , 25000
CLS
DIM SHARED form$, stack(10), avail$, stackSize

avail$ = "1567"
 
addOn

SUB addOn
 IF avail$ = "" AND stackSize = 1 THEN
    IF stack(1) = 21 THEN
     PRINT form$
    END IF
    EXIT SUB
 END IF

 FOR i = 1 TO LEN(avail$)
   avSave$ = avail$

   c$ = MID$(avail$, i, 1)
   avail$ = LEFT$(avail$, i - 1) + MID$(avail$, i + 1)
   IF stackSize = 0 THEN
    stackSize = 1
    stack(1) = VAL(c$)
    form$ = c$
    addOn
    stackSize = 0
    form$ = ""
   ELSE
    IF INSTR("1567", RIGHT$(form$, 1)) THEN
      stack(1) = 10 * stack(1) + VAL(c$)
      form$ = form$ + c$
      addOn
      stack(1) = stack(1) \\ 10
      form$ = LEFT$(form$, LEN(form$) - 1)
     
      stackSize = stackSize + 1
      FOR j = stackSize TO 2 STEP -1
        stack(j) = stack(j - 1)
      NEXT j
      stack(1) = VAL(c$)
      form$ = form$ + "," + c$
      addOn
      stackSize = stackSize - 1
      FOR j = 1 TO stackSize
        stack(j) = stack(j + 1)
      NEXT j
      form$ = LEFT$(form$, LEN(form$) - 2)
     ELSE
      stackSize = stackSize + 1
      FOR j = stackSize TO 2 STEP -1
        stack(j) = stack(j - 1)
      NEXT j
      stack(1) = VAL(c$)
      form$ = form$ + c$
      addOn
      stackSize = stackSize - 1
      FOR j = 1 TO stackSize
        stack(j) = stack(j + 1)
      NEXT j
      form$ = LEFT$(form$, LEN(form$) - 1)
    END IF
   END IF
   avail$ = avSave$
 NEXT i

 IF stackSize >= 2 THEN
   c$ = "+"
   save1 = stack(1): save2 = stack(2)
   stack(1) = stack(1) + stack(2)
   form$ = form$ + c$
   stackSize = stackSize - 1
   FOR j = 2 TO stackSize
     stack(j) = stack(j + 1)
   NEXT
   addOn
   stackSize = stackSize + 1
   FOR j = stackSize TO 3 STEP -1
    stack(j) = stack(j - 1)
   NEXT
   stack(1) = save1: stack(2) = save2
   form$ = LEFT$(form$, LEN(form$) - 1)

   c$ = "-"
   save1 = stack(1): save2 = stack(2)
   stack(1) = stack(2) - stack(1)
   form$ = form$ + c$
   stackSize = stackSize - 1
   FOR j = 2 TO stackSize
     stack(j) = stack(j + 1)
   NEXT
   addOn
   stackSize = stackSize + 1
   FOR j = stackSize TO 3 STEP -1
    stack(j) = stack(j - 1)
   NEXT
   stack(1) = save1: stack(2) = save2
   form$ = LEFT$(form$, LEN(form$) - 1)

   c$ = "*"
   save1 = stack(1): save2 = stack(2)
   stack(1) = stack(1) * stack(2)
   form$ = form$ + c$
   stackSize = stackSize - 1
   FOR j = 2 TO stackSize
     stack(j) = stack(j + 1)
   NEXT
   addOn
   stackSize = stackSize + 1
   FOR j = stackSize TO 3 STEP -1
    stack(j) = stack(j - 1)
   NEXT
   stack(1) = save1: stack(2) = save2
   form$ = LEFT$(form$, LEN(form$) - 1)

   c$ = "/"
   save1 = stack(1): save2 = stack(2)
   IF stack(1) = 0 THEN
    stack(1) = 1D+100
   ELSE
    stack(1) = stack(2) / stack(1)
   END IF
   form$ = form$ + c$
   stackSize = stackSize - 1
   FOR j = 2 TO stackSize
     stack(j) = stack(j + 1)
   NEXT
   addOn
   stackSize = stackSize + 1
   FOR j = stackSize TO 3 STEP -1
    stack(j) = stack(j - 1)
   NEXT
   stack(1) = save1: stack(2) = save2
   form$ = LEFT$(form$, LEN(form$) - 1)

 END IF
END SUB

Finds the RPN 6,1,5,7/-/, which is equivalent to 6 / (1 - 5/7).

Comments: ( You must be logged in to post comments.)
  Subject Author Date
SolutionPuzzle SolutionK Sengupta2022-05-13 11:52:02
re(2): I meanpcbouhid2009-03-12 21:23:25
re: I meanJayDeeKay2009-03-12 21:05:31
I mean "spoiler"!!pcbouhid2009-03-12 15:08:21
Solutionspolierpcbouhid2009-03-12 15:06:59
re(2): one solutionDaniel2009-03-12 12:46:02
re: one solutionSing4TheDay2009-03-12 11:44:47
one solutionDaniel2009-03-12 11:17:00
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (1)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (12)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information