Wpis z mikrobloga

Czasami z nudy uczę się JS i trafiłem na fajne zadanko, jakby ktoś chciał pogimnastykować głowę :D

Consider this puzzle: by starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. How would you write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number? For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice, whereas the number 15 cannot be reached at all.


Rozwiązanie: http://wklej.org/id/2121469/

Źródło: http://eloquentjavascript.net/03_functions.html

#zadaniaprogramistyczne #programowanie #javascript
  • 3
  • Odpowiedz
  • Otrzymuj powiadomienia
    o nowych komentarzach

(defun find-solution (number)
  (cond ((= number 1) "1")
        ((< number 1) (throw 'SEGMENTATION-FAULT-CORE-DUMPED 42))
        ((= 0 (mod number 3))
         (concatenate 'string "(" (find-solution (/ number 3)) "*3)"))
        (t (concatenate 'string "(" (find-solution (- number 5)) "+5)"))))
  • Odpowiedz