Collatz's First Conjecture

2

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!

Khuldraeseth na'Barya

Posted 2017-09-02T12:39:14.913

Reputation: 2 608

Question was closed 2017-09-02T15:47:45.900

one million, nine hundred fifteen thousand, five hundred eighty is of length 63, not 55. The correct result for 1915580 is 6, not 5. – Mr. Xcoder – 2017-09-02T13:17:32.953

@Mr.Xcoder Punctuation is counted. Spaces are not – H.PWiz – 2017-09-02T13:25:41.043

@H.PWiz Thanks for letting me know, I fixed my answer. – Mr. Xcoder – 2017-09-02T13:30:12.820

Do numbers above 10^66 need to be handled? – Nitrodon – 2017-09-02T13:42:59.350

No, will edit soon to address this. – Khuldraeseth na'Barya – 2017-09-02T13:49:20.723

Four has Four Letters – sergiol – 2017-09-02T15:08:18.820

Answers

1

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

Mr. Xcoder

Posted 2017-09-02T12:39:14.913

Reputation: 39 774

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). – Khuldraeseth na'Barya – 2017-09-02T13:49:34.437