showoff.cmn 791 B

123456789101112131415161718192021222324252627282930313233
  1. # program that simplifies fractions
  2. printNumber: # pops and prints a number (0 - 99)
  3. $0 10 > ?
  4. $0 10 / "0" + ->
  5. .
  6. 10 % "0" + ->
  7. .
  8. _simplify: # recursively simplifies fraction, in: nom, denom, divisor
  9. $0 $2 <= $1 $4 <= && ? # divisor not greater than nom and denom?
  10. $1 $1 % 0 = $3 $2 % 0 = && ? # divisor divides nom or denom?
  11. $2 $1 / $:3 # divide both nom and denom
  12. $1 $1 / $:2
  13. ;
  14. ++ # try next divisor
  15. .
  16. _simplify
  17. .
  18. .
  19. simplifyFraction: 2 _simplify ^ .
  20. printFraction: $1 printNumber "/" -> $0 printNumber .
  21. printSimplified: printFraction 0 " = " --> simplifyFraction printFraction 10 -> .
  22. # main program starts here:
  23. 8 4 printSimplified
  24. 55 22 printSimplified
  25. 49 14 printSimplified