{
  "id": "P113",
  "slug": "number-curiosities",
  "key": "P113-number-curiosities",
  "title": "Number curiosities",
  "summary": "What a number's digits do: happy numbers and the cycle every other number falls into, Armstrong numbers, the digital root beside its formula, and Kaprekar's routine to 6174 and 495, step by step.",
  "entry": "main.eml",
  "ui": "terminal",
  "readme": "# P113 - Number curiosities\n\nWhat a number's digits do, four ways: happy numbers, Armstrong numbers,\nthe digital root, and Kaprekar's routine.\n\n- `main.eml` - the menu, one number four ways, the lists, and Kaprekar's\n  routine step by step\n- `curious.eml` - digits, the happy chain, Armstrong sums, digital roots\n  and Kaprekar's steps\n\nHow each part works:\n\n- Happy numbers, as in the corpus case `happy-number`: replace a number by\n  the sum of the squares of its digits, again and again. A happy number\n  reaches 1. Every other number falls into the cycle 4, 16, 37, 58, 89, 145,\n  42, 20 and never reaches 1. The chain stops at the first number seen\n  twice.\n- Armstrong numbers, as in the corpus case `armstrong-number-checker`: the\n  digits, each raised to the number of digits, add up to the number itself.\n  Up to 10,000 there are 16: the nine single digits, 153, 370, 371, 407,\n  1634, 8208 and 9474.\n- The digital root, as in the corpus case `digital-root-recursive`: add\n  the digits until one is left. It equals 1 + (n - 1) mod 9, and the\n  program shows both.\n- Kaprekar's routine: sort the digits high to low, subtract them sorted\n  low to high, and repeat, keeping zeros in front. Every number from 1000\n  to 9999 whose digits are not all the same reaches 6174 within 7 steps,\n  and every one from 100 to 999 reaches 495 within 6. That was found by\n  trying all 8,991 and all 891 of them, in the cross-check; the program\n  states the result.\n\nWhat is checked: menu choices 1 to 5; a number from 1 to 999,999,999; up\nto 1,000 for the happy list and 10,000 for the Armstrong list; 3 or 4\ndigits for Kaprekar's routine, zeros in front allowed. An empty answer\ncancels.\n\nSessions: `sessions/basic.in` gives:\n- 7 (happy, Armstrong) and 4 (unhappy, inside the cycle);\n- 153 (Armstrong, Kaprekar to 495 in 3 steps), 3087, 9474 (Armstrong,\n  5 steps to 6174) and 999,999,999;\n- the happy numbers to 100 and the Armstrong numbers to 10,000;\n- Kaprekar's routine on 0021, 495, 2111 (through 0999) and 5555 (all\n  digits the same).\n\n`sessions/bad-input.in` gives:\n- menu choices 0 and x;\n- numbers 0, 1,000,000,000 and abc, then a cancel;\n- list limits 0, 1,001 and 10,001, with cancels;\n- Kaprekar inputs: a cancel, 12, 12345, abc, and 000.\n\nBuilt on the verified corpus cases `happy-number` (the sum of squared\ndigits, followed to 1 or to a cycle), `armstrong-number-checker` (digits\nto the power of their count) and `digital-root-recursive` (digit sums\ndown to one digit).\n",
  "modules": [
    {
      "name": "main.eml",
      "eml": "# P113 number curiosities: one number's digits four ways, happy and\n# Armstrong numbers up to a limit, and Kaprekar's routine step by step.\nimport curious\n\ndef plain(s):\n    0 => i\n    len(s) => j\n    while i < j and s[i] == \" \":\n        i + 1 => i\n    while j > i and s[j - 1] == \" \":\n        j - 1 => j\n    return s[i:j]\n\ndef number(s, most):\n    if len(s) == 0 or len(s) > most:\n        return -1\n    0 => n\n    for c in s:\n        if not (c >= \"0\" and c <= \"9\"):\n            return -1\n        n * 10 + int(c) => n\n    return n\n\ndef ask(prompt, low, high):\n    while True:\n        plain(input(prompt + \" (\" + str(low) + \" to \" + str(high) + \")> \")) => a\n        if a == \"\":\n            return -1\n        number(a, 10) => n\n        if n >= low and n <= high:\n            return n\n        (\"Type a whole number from \" + str(low) + \" to \" + str(high) + \".\") ^0\n\ndef arrows(xs):\n    \"\" => out\n    for x in xs:\n        if out != \"\":\n            out + \" -> \" => out\n        out + str(x) => out\n    return out\n\ndef padded(n, width):\n    str(n) => s\n    while len(s) < width:\n        \"0\" + s => s\n    return s\n\ndef one():\n    ask(\"number\", 1, 999999999) => n\n    if n == -1:\n        \"Cancelled.\" ^0\n        return\n    curious.happy_chain(n) => h\n    if h[0]:\n        (\"Happy: \" + arrows(h[1]) + \".\") ^0\n    else:\n        (\"Not happy: \" + arrows(h[1]) + \", round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\") ^0\n    curious.digits(n) => ds\n    curious.armstrong_sum(n) => a\n    \"\" => terms\n    for d in ds:\n        if terms != \"\":\n            terms + \" + \" => terms\n        terms + str(d) + \"^\" + str(len(ds)) => terms\n    if a == n:\n        (\"Armstrong: \" + terms + \" = \" + str(a) + \", the number itself.\") ^0\n    else:\n        (\"Not Armstrong: \" + terms + \" = \" + str(a) + \".\") ^0\n    curious.root_steps(n) => r\n    (\"Digital root \" + str(r[len(r) - 1]) + \": \" + arrows(r) + \"; and 1 + (\" + str(n) + \" - 1) mod 9 = \" + str(curious.root_formula(n)) + \".\") ^0\n    if len(ds) == 3 or len(ds) == 4:\n        kap(n, len(ds))\n\ndef kap(n, width):\n    if curious.same_digits(n, width):\n        (\"Kaprekar's routine needs at least two different digits; \" + padded(n, width) + \" goes straight to \" + padded(0, width) + \".\") ^0\n        return\n    curious.kaprekar(n, width) => st\n    (\"Kaprekar's routine on \" + str(width) + \" digits:\") ^0\n    0 => k\n    -1 => reached\n    for s in st:\n        k + 1 => k\n        (\"  \" + padded(s[0], width) + \" - \" + padded(s[1], width) + \" = \" + padded(s[2], width)) ^0\n        if reached == -1 and (s[2] == 6174 or s[2] == 495):\n            k => reached\n    if reached >= 0:\n        (\"It reaches \" + str(st[reached - 1][2]) + \" in \" + str(reached) + \" step\" + plural(reached) + \", and \" + str(st[reached - 1][2]) + \" gives itself again.\") ^0\n\ndef plural(n):\n    if n == 1:\n        return \"\"\n    return \"s\"\n\ndef list_happy():\n    ask(\"up to\", 1, 1000) => n\n    if n == -1:\n        \"Cancelled.\" ^0\n        return\n    [] => hs\n    for k in [1:n]:\n        if curious.happy_chain(k)[0]:\n            hs + [k] => hs\n    \"\" => line\n    for h in hs:\n        if line != \"\":\n            line + \" \" => line\n        line + str(h) => line\n    (str(len(hs)) + \" happy numbers up to \" + str(n) + \": \" + line) ^0\n    \"Every other number falls into the cycle 4, 16, 37, 58, 89, 145, 42, 20 and never reaches 1.\" ^0\n\ndef list_armstrong():\n    ask(\"up to\", 1, 10000) => n\n    if n == -1:\n        \"Cancelled.\" ^0\n        return\n    \"\" => line\n    0 => c\n    for k in [1:n]:\n        if curious.armstrong_sum(k) == k:\n            c + 1 => c\n            if line != \"\":\n                line + \" \" => line\n            line + str(k) => line\n    (str(c) + \" Armstrong numbers up to \" + str(n) + \": \" + line) ^0\n    \"Each single digit is one; after them the next are 153, 370, 371 and 407, then 1634, 8208 and 9474.\" ^0\n\ndef kaprekar_menu():\n    plain(input(\"a number of 3 or 4 digits, zeros in front allowed (such as 0021)> \")) => t\n    if t == \"\":\n        \"Cancelled.\" ^0\n        return\n    number(t, 4) => n\n    if n < 0 or len(t) < 3:\n        \"Type 3 or 4 digits.\" ^0\n        return\n    kap(n, len(t))\n    if len(t) == 4:\n        \"Every number from 1000 to 9999 whose digits are not all the same reaches 6174 within 7 steps.\" ^0\n    else:\n        \"Every number from 100 to 999 whose digits are not all the same reaches 495 within 6 steps.\" ^0\n\n\"== Number curiosities ==\" ^0\n\"What a number's digits do: happy numbers, Armstrong numbers, the digital root and Kaprekar's routine.\" ^0\nTrue => running\nwhile running:\n    \"\" ^0\n    \"1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\" ^0\n    plain(input(\"choice> \")) => choice\n    if choice == \"1\":\n        one()\n    elif choice == \"2\":\n        list_happy()\n    elif choice == \"3\":\n        list_armstrong()\n    elif choice == \"4\":\n        kaprekar_menu()\n    elif choice == \"5\":\n        False => running\n    else:\n        \"Pick a number from 1 to 5.\" ^0\n\"Bye.\" ^0\n",
      "python": "import curious\n\ndef plain(s):\n    i = 0\n    j = len(s)\n    while i < j and s[i] == \" \":\n        i = i + 1\n    while j > i and s[j - 1] == \" \":\n        j = j - 1\n    return s[i:j]\n\ndef number(s, most):\n    if len(s) == 0 or len(s) > most:\n        return -1\n    n = 0\n    for c in s:\n        if not (c >= \"0\" and c <= \"9\"):\n            return -1\n        n = n * 10 + int(c)\n    return n\n\ndef ask(prompt, low, high):\n    while True:\n        a = plain(input(prompt + \" (\" + str(low) + \" to \" + str(high) + \")> \"))\n        if a == \"\":\n            return -1\n        n = number(a, 10)\n        if n >= low and n <= high:\n            return n\n        print(\"Type a whole number from \" + str(low) + \" to \" + str(high) + \".\")\n\ndef arrows(xs):\n    out = \"\"\n    for x in xs:\n        if out != \"\":\n            out = out + \" -> \"\n        out = out + str(x)\n    return out\n\ndef padded(n, width):\n    s = str(n)\n    while len(s) < width:\n        s = \"0\" + s\n    return s\n\ndef one():\n    n = ask(\"number\", 1, 999999999)\n    if n == -1:\n        print(\"Cancelled.\")\n        return\n    h = curious.happy_chain(n)\n    if h[0]:\n        print(\"Happy: \" + arrows(h[1]) + \".\")\n    else:\n        print(\"Not happy: \" + arrows(h[1]) + \", round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\")\n    ds = curious.digits(n)\n    a = curious.armstrong_sum(n)\n    terms = \"\"\n    for d in ds:\n        if terms != \"\":\n            terms = terms + \" + \"\n        terms = terms + str(d) + \"^\" + str(len(ds))\n    if a == n:\n        print(\"Armstrong: \" + terms + \" = \" + str(a) + \", the number itself.\")\n    else:\n        print(\"Not Armstrong: \" + terms + \" = \" + str(a) + \".\")\n    r = curious.root_steps(n)\n    print(\"Digital root \" + str(r[len(r) - 1]) + \": \" + arrows(r) + \"; and 1 + (\" + str(n) + \" - 1) mod 9 = \" + str(curious.root_formula(n)) + \".\")\n    if len(ds) == 3 or len(ds) == 4:\n        kap(n, len(ds))\n\ndef kap(n, width):\n    if curious.same_digits(n, width):\n        print(\"Kaprekar's routine needs at least two different digits; \" + padded(n, width) + \" goes straight to \" + padded(0, width) + \".\")\n        return\n    st = curious.kaprekar(n, width)\n    print(\"Kaprekar's routine on \" + str(width) + \" digits:\")\n    k = 0\n    reached = -1\n    for s in st:\n        k = k + 1\n        print(\"  \" + padded(s[0], width) + \" - \" + padded(s[1], width) + \" = \" + padded(s[2], width))\n        if reached == -1 and (s[2] == 6174 or s[2] == 495):\n            reached = k\n    if reached >= 0:\n        print(\"It reaches \" + str(st[reached - 1][2]) + \" in \" + str(reached) + \" step\" + plural(reached) + \", and \" + str(st[reached - 1][2]) + \" gives itself again.\")\n\ndef plural(n):\n    if n == 1:\n        return \"\"\n    return \"s\"\n\ndef list_happy():\n    n = ask(\"up to\", 1, 1000)\n    if n == -1:\n        print(\"Cancelled.\")\n        return\n    hs = []\n    for k in range(1, n+1):\n        if curious.happy_chain(k)[0]:\n            hs = hs + [k]\n    line = \"\"\n    for h in hs:\n        if line != \"\":\n            line = line + \" \"\n        line = line + str(h)\n    print(str(len(hs)) + \" happy numbers up to \" + str(n) + \": \" + line)\n    print(\"Every other number falls into the cycle 4, 16, 37, 58, 89, 145, 42, 20 and never reaches 1.\")\n\ndef list_armstrong():\n    n = ask(\"up to\", 1, 10000)\n    if n == -1:\n        print(\"Cancelled.\")\n        return\n    line = \"\"\n    c = 0\n    for k in range(1, n+1):\n        if curious.armstrong_sum(k) == k:\n            c = c + 1\n            if line != \"\":\n                line = line + \" \"\n            line = line + str(k)\n    print(str(c) + \" Armstrong numbers up to \" + str(n) + \": \" + line)\n    print(\"Each single digit is one; after them the next are 153, 370, 371 and 407, then 1634, 8208 and 9474.\")\n\ndef kaprekar_menu():\n    t = plain(input(\"a number of 3 or 4 digits, zeros in front allowed (such as 0021)> \"))\n    if t == \"\":\n        print(\"Cancelled.\")\n        return\n    n = number(t, 4)\n    if n < 0 or len(t) < 3:\n        print(\"Type 3 or 4 digits.\")\n        return\n    kap(n, len(t))\n    if len(t) == 4:\n        print(\"Every number from 1000 to 9999 whose digits are not all the same reaches 6174 within 7 steps.\")\n    else:\n        print(\"Every number from 100 to 999 whose digits are not all the same reaches 495 within 6 steps.\")\n\nprint(\"== Number curiosities ==\")\nprint(\"What a number's digits do: happy numbers, Armstrong numbers, the digital root and Kaprekar's routine.\")\nrunning = True\nwhile running:\n    print(\"\")\n    print(\"1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\")\n    choice = plain(input(\"choice> \"))\n    if choice == \"1\":\n        one()\n    elif choice == \"2\":\n        list_happy()\n    elif choice == \"3\":\n        list_armstrong()\n    elif choice == \"4\":\n        kaprekar_menu()\n    elif choice == \"5\":\n        running = False\n    else:\n        print(\"Pick a number from 1 to 5.\")\nprint(\"Bye.\")\n"
    },
    {
      "name": "curious.eml",
      "eml": "# P113 number curiosities - four things a number's digits do:\n#   happy numbers: replace a number by the sum of the squares of its digits;\n#     it reaches 1 (happy) or falls into 4, 16, 37, 58, 89, 145, 42, 20\n#     (the corpus case happy-number);\n#   Armstrong numbers: the sum of the digits, each raised to the number of\n#     digits, is the number itself (the corpus case armstrong-number-checker);\n#   the digital root: add the digits until one is left; it is 1 + (n - 1)\n#     mod 9 for n above 0 (the corpus case digital-root-recursive);\n#   Kaprekar's routine: the digits sorted high to low, minus sorted low to\n#     high, again and again; four digits reach 6174, three reach 495.\n\ndef digits(n):\n    # most significant first\n    [] => out\n    n => x\n    if x == 0:\n        return [0]\n    while x > 0:\n        [x % 10] + out => out\n        int((x - x % 10) / 10) => x\n    return out\n\ndef square_sum(n):\n    0 => s\n    for d in digits(n):\n        s + d * d => s\n    return s\n\ndef happy_chain(n):\n    # [happy, the numbers visited]: stops at 1, or at the first number seen\n    # twice\n    [n] => seen\n    n => x\n    while x != 1:\n        square_sum(x) => x\n        for y in seen:\n            if y == x:\n                return [False, seen + [x]]\n        seen + [x] => seen\n    return [True, seen]\n\ndef power(b, e):\n    1 => p\n    for i in [1:e]:\n        p * b => p\n    return p\n\ndef armstrong_sum(n):\n    digits(n) => ds\n    0 => s\n    for d in ds:\n        s + power(d, len(ds)) => s\n    return s\n\ndef root_steps(n):\n    # the digit sums down to one digit, n first\n    [n] => out\n    n => x\n    while x > 9:\n        0 => s\n        for d in digits(x):\n            s + d => s\n        s => x\n        out + [x] => out\n    return out\n\ndef root_formula(n):\n    if n == 0:\n        return 0\n    return 1 + (n - 1) % 9\n\ndef kaprekar_step(n, width):\n    # [big, small, big - small], with leading zeros kept to `width` digits\n    digits(n) => ds\n    while len(ds) < width:\n        [0] + ds => ds\n    # sort low to high\n    for i in [1:len(ds) - 1]:\n        i => j\n        while j > 0 and ds[j] < ds[j - 1]:\n            ds[j] => t\n            ds[j - 1] => ds[j]\n            t => ds[j - 1]\n            j - 1 => j\n    0 => small\n    0 => big\n    for d in ds:\n        small * 10 + d => small\n    for k in [0:len(ds) - 1]:\n        big * 10 + ds[len(ds) - 1 - k] => big\n    return [big, small, big - small]\n\ndef kaprekar(n, width):\n    # the steps until the number repeats: [[big, small, result], ...]\n    [] => out\n    [n] => seen\n    n => x\n    True => going\n    while going:\n        kaprekar_step(x, width) => st\n        out + [st] => out\n        st[2] => x\n        for y in seen:\n            if y == x:\n                False => going\n        seen + [x] => seen\n    return out\n\ndef same_digits(n, width):\n    digits(n) => ds\n    while len(ds) < width:\n        [0] + ds => ds\n    for d in ds:\n        if d != ds[0]:\n            return False\n    return True\n",
      "python": "def digits(n):\n    out = []\n    x = n\n    if x == 0:\n        return [0]\n    while x > 0:\n        out = [x % 10] + out\n        x = int((x - x % 10) / 10)\n    return out\n\ndef square_sum(n):\n    s = 0\n    for d in digits(n):\n        s = s + d * d\n    return s\n\ndef happy_chain(n):\n    seen = [n]\n    x = n\n    while x != 1:\n        x = square_sum(x)\n        for y in seen:\n            if y == x:\n                return [False, seen + [x]]\n        seen = seen + [x]\n    return [True, seen]\n\ndef power(b, e):\n    p = 1\n    for i in range(1, e+1):\n        p = p * b\n    return p\n\ndef armstrong_sum(n):\n    ds = digits(n)\n    s = 0\n    for d in ds:\n        s = s + power(d, len(ds))\n    return s\n\ndef root_steps(n):\n    out = [n]\n    x = n\n    while x > 9:\n        s = 0\n        for d in digits(x):\n            s = s + d\n        x = s\n        out = out + [x]\n    return out\n\ndef root_formula(n):\n    if n == 0:\n        return 0\n    return 1 + (n - 1) % 9\n\ndef kaprekar_step(n, width):\n    ds = digits(n)\n    while len(ds) < width:\n        ds = [0] + ds\n    for i in range(1, len(ds)):\n        j = i\n        while j > 0 and ds[j] < ds[j - 1]:\n            t = ds[j]\n            ds[j] = ds[j - 1]\n            ds[j - 1] = t\n            j = j - 1\n    small = 0\n    big = 0\n    for d in ds:\n        small = small * 10 + d\n    for k in range(0, len(ds)):\n        big = big * 10 + ds[len(ds) - 1 - k]\n    return [big, small, big - small]\n\ndef kaprekar(n, width):\n    out = []\n    seen = [n]\n    x = n\n    going = True\n    while going:\n        st = kaprekar_step(x, width)\n        out = out + [st]\n        x = st[2]\n        for y in seen:\n            if y == x:\n                going = False\n        seen = seen + [x]\n    return out\n\ndef same_digits(n, width):\n    ds = digits(n)\n    while len(ds) < width:\n        ds = [0] + ds\n    for d in ds:\n        if d != ds[0]:\n            return False\n    return True\n"
    }
  ],
  "sessions": [
    {
      "name": "bad-input",
      "input": "0\nx\n1\n0\n1000000000\nabc\n\n2\n0\n1001\n\n3\n10001\n\n4\n\n4\n12\n4\n12345\n4\nabc\n4\n000\n5\n",
      "screen": "== Number curiosities ==\nWhat a number's digits do: happy numbers, Armstrong numbers, the digital root and Kaprekar's routine.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 0\nPick a number from 1 to 5.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> x\nPick a number from 1 to 5.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 0\nType a whole number from 1 to 999999999.\nnumber (1 to 999999999)> 1000000000\nType a whole number from 1 to 999999999.\nnumber (1 to 999999999)> abc\nType a whole number from 1 to 999999999.\nnumber (1 to 999999999)> \nCancelled.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 2\nup to (1 to 1000)> 0\nType a whole number from 1 to 1000.\nup to (1 to 1000)> 1001\nType a whole number from 1 to 1000.\nup to (1 to 1000)> \nCancelled.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 3\nup to (1 to 10000)> 10001\nType a whole number from 1 to 10000.\nup to (1 to 10000)> \nCancelled.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> \nCancelled.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 12\nType 3 or 4 digits.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 12345\nType 3 or 4 digits.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> abc\nType 3 or 4 digits.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 000\nKaprekar's routine needs at least two different digits; 000 goes straight to 000.\nEvery number from 100 to 999 whose digits are not all the same reaches 495 within 6 steps.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 5\nBye.\n",
      "interpreter": "equal"
    },
    {
      "name": "basic",
      "input": "1\n7\n1\n4\n1\n153\n1\n3087\n1\n9474\n1\n999999999\n2\n100\n3\n10000\n4\n0021\n4\n495\n4\n2111\n4\n5555\n5\n",
      "screen": "== Number curiosities ==\nWhat a number's digits do: happy numbers, Armstrong numbers, the digital root and Kaprekar's routine.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 7\nHappy: 7 -> 49 -> 97 -> 130 -> 10 -> 1.\nArmstrong: 7^1 = 7, the number itself.\nDigital root 7: 7; and 1 + (7 - 1) mod 9 = 7.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 4\nNot happy: 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4, round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\nArmstrong: 4^1 = 4, the number itself.\nDigital root 4: 4; and 1 + (4 - 1) mod 9 = 4.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 153\nNot happy: 153 -> 35 -> 34 -> 25 -> 29 -> 85 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89, round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\nArmstrong: 1^3 + 5^3 + 3^3 = 153, the number itself.\nDigital root 9: 153 -> 9; and 1 + (153 - 1) mod 9 = 9.\nKaprekar's routine on 3 digits:\n  531 - 135 = 396\n  963 - 369 = 594\n  954 - 459 = 495\n  954 - 459 = 495\nIt reaches 495 in 3 steps, and 495 gives itself again.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 3087\nNot happy: 3087 -> 122 -> 9 -> 81 -> 65 -> 61 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37, round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\nNot Armstrong: 3^4 + 0^4 + 8^4 + 7^4 = 6578.\nDigital root 9: 3087 -> 18 -> 9; and 1 + (3087 - 1) mod 9 = 9.\nKaprekar's routine on 4 digits:\n  8730 - 0378 = 8352\n  8532 - 2358 = 6174\n  7641 - 1467 = 6174\nIt reaches 6174 in 2 steps, and 6174 gives itself again.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 9474\nNot happy: 9474 -> 162 -> 41 -> 17 -> 50 -> 25 -> 29 -> 85 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89, round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\nArmstrong: 9^4 + 4^4 + 7^4 + 4^4 = 9474, the number itself.\nDigital root 6: 9474 -> 24 -> 6; and 1 + (9474 - 1) mod 9 = 6.\nKaprekar's routine on 4 digits:\n  9744 - 4479 = 5265\n  6552 - 2556 = 3996\n  9963 - 3699 = 6264\n  6642 - 2466 = 4176\n  7641 - 1467 = 6174\n  7641 - 1467 = 6174\nIt reaches 6174 in 5 steps, and 6174 gives itself again.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 1\nnumber (1 to 999999999)> 999999999\nNot happy: 999999999 -> 729 -> 134 -> 26 -> 40 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16, round the cycle of 4, 16, 37, 58, 89, 145, 42 and 20.\nNot Armstrong: 9^9 + 9^9 + 9^9 + 9^9 + 9^9 + 9^9 + 9^9 + 9^9 + 9^9 = 3486784401.\nDigital root 9: 999999999 -> 81 -> 9; and 1 + (999999999 - 1) mod 9 = 9.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 2\nup to (1 to 1000)> 100\n20 happy numbers up to 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100\nEvery other number falls into the cycle 4, 16, 37, 58, 89, 145, 42, 20 and never reaches 1.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 3\nup to (1 to 10000)> 10000\n16 Armstrong numbers up to 10000: 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474\nEach single digit is one; after them the next are 153, 370, 371 and 407, then 1634, 8208 and 9474.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 0021\nKaprekar's routine on 4 digits:\n  2100 - 0012 = 2088\n  8820 - 0288 = 8532\n  8532 - 2358 = 6174\n  7641 - 1467 = 6174\nIt reaches 6174 in 3 steps, and 6174 gives itself again.\nEvery number from 1000 to 9999 whose digits are not all the same reaches 6174 within 7 steps.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 495\nKaprekar's routine on 3 digits:\n  954 - 459 = 495\nIt reaches 495 in 1 step, and 495 gives itself again.\nEvery number from 100 to 999 whose digits are not all the same reaches 495 within 6 steps.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 2111\nKaprekar's routine on 4 digits:\n  2111 - 1112 = 0999\n  9990 - 0999 = 8991\n  9981 - 1899 = 8082\n  8820 - 0288 = 8532\n  8532 - 2358 = 6174\n  7641 - 1467 = 6174\nIt reaches 6174 in 5 steps, and 6174 gives itself again.\nEvery number from 1000 to 9999 whose digits are not all the same reaches 6174 within 7 steps.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 4\na number of 3 or 4 digits, zeros in front allowed (such as 0021)> 5555\nKaprekar's routine needs at least two different digits; 5555 goes straight to 0000.\nEvery number from 1000 to 9999 whose digits are not all the same reaches 6174 within 7 steps.\n\n1) one number  2) happy numbers  3) Armstrong numbers  4) Kaprekar's routine  5) quit\nchoice> 5\nBye.\n",
      "interpreter": "equal"
    }
  ],
  "builtOn": [
    {
      "slug": "happy-number",
      "caseId": "087-happy-number",
      "title": "Happy number"
    },
    {
      "slug": "armstrong-number-checker",
      "caseId": "012-armstrong-number-checker",
      "title": "Armstrong number checker"
    },
    {
      "slug": "digital-root-recursive",
      "caseId": "068-digital-root-recursive",
      "title": "Digital root (recursive)"
    }
  ],
  "updated": "2026-10-16"
}
