2
\$\begingroup\$

Background

The Collatz Conjecture is quite well-known. Take any natural number. Triple and increment if odd; cut in half if even. Repeat, and it will reach 1 eventually. This famous conjecture, however, is only that, for it is yet unproven.

Little-known to the world, that was not Lothar Collatz's first attempt at fame. His first Conjecture (now proven) was a touch simpler:

  • Take any natural number n.
  • While n ≠ 4, find the length of its written-out form (English). Store this value to n.

The conjecture is that all natural numbers will eventually become 4. This did not catch on as well as Collatz had hoped, so he returned to the drawing board.

Your task

Read a positive integer n < 2147483648 (2^31) from input and return/print the number of iterations required to reach 4.

Things to note

Do not use and. 228 is "two hundred twenty-eight," not "two hundred and twenty-eight."

Do not use articles. 145 is "one hundred forty-five," not "a hundred forty-five.)

Commas set apart each three orders or magnitude: "one million, four hundred eighty-two thousand, one hundred seventy-five."

Punctuation is counted. Spaces are not. Hyphenate 21-99 except multiples of ten.

Test cases

4 (four): 0
3 (three→five→four): 2
6278 (six thousand, two hundred seventy-eight→thirty-five→eleven→six→three→five→four): 6
1915580 (one million, nine hundred fifteen thousand, five hundred eighty→fifty-five→ten→three→five→four): 5
2147483647 (two billion, one hundred forty-seven million, four hundred eighty-three thousand, six hundred forty-seven→ninety-three→twelve→six→three→five→four): 6

This is , so the shortest code (in bytes) in each language WINS! Good luck!

\$\endgroup\$
6
  • \$\begingroup\$ one million, nine hundred fifteen thousand, five hundred eighty is of length 63, not 55. The correct result for 1915580 is 6, not 5. \$\endgroup\$
    – Mr. Xcoder
    Commented Sep 2, 2017 at 13:17
  • \$\begingroup\$ @Mr.Xcoder Punctuation is counted. Spaces are not \$\endgroup\$
    – H.PWiz
    Commented Sep 2, 2017 at 13:25
  • \$\begingroup\$ @H.PWiz Thanks for letting me know, I fixed my answer. \$\endgroup\$
    – Mr. Xcoder
    Commented Sep 2, 2017 at 13:30
  • \$\begingroup\$ Do numbers above 10^66 need to be handled? \$\endgroup\$
    – Nitrodon
    Commented Sep 2, 2017 at 13:42
  • \$\begingroup\$ No, will edit soon to address this. \$\endgroup\$ Commented Sep 2, 2017 at 13:49

1 Answer 1

1
\$\begingroup\$

Python 3 + inflect,  122  114 bytes

Saved 8 bytes thanks to Scrooble.

from inflect import*
f=lambda k,i=0:k!=4and f(len(engine().number_to_words(k,andword="").replace(" ","")),i+1)or i

This cannot be tested online because of the package it uses. Running the test suite gives:

4 -> 0
3 -> 2
6278 -> 6
1915580 -> 5
\$\endgroup\$
1
  • \$\begingroup\$ I'm unfamiliar with inflect, but from reading the linked page, I believe that this could be shortened by using number_to_words(k,andword="") instead of number_to_words(k).replace(" and","") (saves 8 bytes). \$\endgroup\$ Commented Sep 2, 2017 at 13:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.