/extra/project-euler/089/089.factor

http://github.com/abeaumont/factor · Factor · 48 lines · 8 code · 12 blank · 28 comment · 0 complexity · 10c4e1aa88187e8573bd1acf578cdcdf MD5 · raw file

  1. ! Copyright (c) 2009 Doug Coleman.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: io.encodings.ascii io.files kernel math
  4. project-euler.common roman sequences ;
  5. IN: project-euler.089
  6. ! http://projecteuler.net/index.php?section=problems&id=089
  7. ! DESCRIPTION
  8. ! -----------
  9. ! The rules for writing Roman numerals allow for many ways of writing
  10. ! each number (see FAQ: Roman Numerals). However, there is always a
  11. ! "best" way of writing a particular number.
  12. ! For example, the following represent all of the legitimate ways of
  13. ! writing the number sixteen:
  14. ! IIIIIIIIIIIIIIII
  15. ! VIIIIIIIIIII
  16. ! VVIIIIII
  17. ! XIIIIII
  18. ! VVVI
  19. ! XVI
  20. ! The last example being considered the most efficient, as it uses
  21. ! the least number of numerals.
  22. ! The 11K text file, roman.txt (right click and 'Save Link/Target As...'),
  23. ! contains one thousand numbers written in valid, but not necessarily
  24. ! minimal, Roman numerals; that is, they are arranged in descending units
  25. ! and obey the subtractive pair rule (see FAQ for the definitive rules
  26. ! for this problem).
  27. ! Find the number of characters saved by writing each of these in their minimal form.
  28. ! SOLUTION
  29. ! --------
  30. : euler089 ( -- n )
  31. "resource:extra/project-euler/089/roman.txt" ascii file-lines
  32. [ ] [ [ roman> >roman ] map ] bi
  33. [ [ length ] map-sum ] bi@ - ;
  34. ! [ euler089 ] 100 ave-time
  35. ! 14 ms ave run time - 0.27 SD (100 trials)
  36. SOLUTION: euler089