--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 45,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 45,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "load 'array-numbers.rb'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 46,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "38890"
+ ]
+ },
+ "execution_count": 46,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch = (0..9999).flat_map {|i| i.to_digits}\n",
+ "ch.length"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 47,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[9, 7, 9, 9, 9, 8, 9, 9, 9, 9]"
+ ]
+ },
+ "execution_count": 47,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch[-10..-1]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 48,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 48,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch[1]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 49,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 49,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch[10]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 50,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "5"
+ ]
+ },
+ "execution_count": 50,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch[100]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 51,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "38890"
+ ]
+ },
+ "execution_count": 51,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ch.length"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 52,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":champ"
+ ]
+ },
+ "execution_count": 52,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def champ(i)\n",
+ " if i < 10\n",
+ " i\n",
+ " elsif i < 190\n",
+ " if (i - 10) % 2 == 0 # tens\n",
+ " ((i - 10) / 2) / 10 + 1\n",
+ " else # units\n",
+ " # ((i - 11) / 2) % 10\n",
+ " ((i - 10) / 2) % 10\n",
+ " end\n",
+ " elsif i < 2890\n",
+ " if (i - 190) % 3 == 0 #hundreds\n",
+ " ((i - 190) / 3) / 100 + 1\n",
+ " elsif (i - 190) % 3 == 1 # tens\n",
+ " (((i - 190) / 3) / 10) % 10\n",
+ " else # units\n",
+ " ((i - 190) / 3) % 10\n",
+ " end\n",
+ " elsif i < 38890\n",
+ " if (i - 2890) % 4 == 0 # thousands\n",
+ " ((i - 2890) / 4) / 1000 + 1\n",
+ " elsif (i - 2890) % 4 == 1 # hundreds\n",
+ " (((i - 2890) / 4) / 100) % 10\n",
+ " elsif (i - 2890) % 4 == 2 # tens\n",
+ " (((i - 2890) / 4) / 10) % 10\n",
+ " else # units\n",
+ " ((i - 2890) / 4) % 10\n",
+ " end\n",
+ " else\n",
+ " 99\n",
+ " end\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 105,
+ "metadata": {
+ "collapsed": false,
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1 :: 1 (2890)\n",
+ "0 :: 0 (2891)\n",
+ "0 :: 0 (2892)\n",
+ "0 :: 0 (2893)\n",
+ "1 :: 1 (2894)\n",
+ "0 :: 0 (2895)\n",
+ "0 :: 0 (2896)\n",
+ "1 :: 1 (2897)\n",
+ "1 :: 1 (2898)\n",
+ "0 :: 0 (2899)\n",
+ "0 :: 0 (2900)\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "2890..2900"
+ ]
+ },
+ "execution_count": 105,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(2890..2900).each do |i|\n",
+ " puts \"#{champ i} :: #{ch[i]} (#{i})\"\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 95,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 95,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(1..38889).all? {|i| champ(i) == ch[i]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 107,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[]"
+ ]
+ },
+ "execution_count": 107,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(1..38889).select {|i| champ(i) != ch[i]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 108,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 108,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "190 - 10 == 2 * 9 * 10 ** 1"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 109,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 109,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "2890 - 190 == 3 * 9 * 10 ** 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 110,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 110,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "38890 - 2890 == 4 * 9 * 10 ** 3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 120,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[1, 10, 190, 2890, 38890, 488890, 5888890]"
+ ]
+ },
+ "execution_count": 120,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# threshold, number of digits, offset\n",
+ "(1..6).map {|n| n * 9 * 10**(n-1)}.reduce([1, 0, 0]) {|ss, n| ss + [[ss[-1][0] + n]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[1, 0, 0, [9], [189], [2889], [38889], [488889], [5888889]]"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(1..6).map {|n| n * 9 * 10**(n-1)}.reduce([[1, 0, 0]]) {|ss, n| ss + [[ss[-1][0] + n, n, ]]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[[0, 9], [1, 180], [2, 2700], [3, 36000], [4, 450000], [5, 5400000], [6, 63000000]]"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(0..6).map {|n| [n, (n+1) * 9 * 10**n]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[[0, 1, 1], [9, 0, 10], [189, 1, 190], [2889, 2, 2890], [38889, 3, 38890], [488889, 4, 488890], [5888889, 5, 5888890], [68888889, 6, 68888890]]"
+ ]
+ },
+ "execution_count": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(0..6).map {|n| [n, (n+1) * 9 * 10**n]}.reduce([[0, 1, 1]]) {|ts, ns| ts + [[ts[-1][0] + ns[1], ns[0], ts[-1][2] + ns[1]]] }"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 94,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":champ2"
+ ]
+ },
+ "execution_count": 94,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def champ2(n)\n",
+ " i = 1\n",
+ " limit = 10\n",
+ " offset = 0\n",
+ " while n >= limit\n",
+ " offset = limit\n",
+ " i += 1\n",
+ " step = i * 9 * 10**(i-1)\n",
+ " limit += step\n",
+ " end\n",
+ " unit = (n - offset) % i\n",
+ " digit = ((n - offset) / i) / (10 ** (i-(unit +1))) % 10\n",
+ " digit += 1 if unit == 0 && i != 1\n",
+ " # return i, limit, offset, unit, digit\n",
+ " digit\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 91,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1 : 1 :: [1, 10, 0, 0, 1]\n",
+ "2 : 2 :: [1, 10, 0, 0, 2]\n",
+ "3 : 3 :: [1, 10, 0, 0, 3]\n",
+ "4 : 4 :: [1, 10, 0, 0, 4]\n",
+ "5 : 5 :: [1, 10, 0, 0, 5]\n",
+ "6 : 6 :: [1, 10, 0, 0, 6]\n",
+ "7 : 7 :: [1, 10, 0, 0, 7]\n",
+ "8 : 8 :: [1, 10, 0, 0, 8]\n",
+ "9 : 9 :: [1, 10, 0, 0, 9]\n",
+ "10 : 1 :: [2, 190, 10, 0, 1]\n",
+ "11 : 0 :: [2, 190, 10, 1, 0]\n",
+ "12 : 1 :: [2, 190, 10, 0, 1]\n",
+ "13 : 1 :: [2, 190, 10, 1, 1]\n",
+ "14 : 1 :: [2, 190, 10, 0, 1]\n",
+ "15 : 2 :: [2, 190, 10, 1, 2]\n",
+ "16 : 1 :: [2, 190, 10, 0, 1]\n",
+ "17 : 3 :: [2, 190, 10, 1, 3]\n",
+ "18 : 1 :: [2, 190, 10, 0, 1]\n",
+ "19 : 4 :: [2, 190, 10, 1, 4]\n",
+ "20 : 1 :: [2, 190, 10, 0, 1]\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "1..20"
+ ]
+ },
+ "execution_count": 91,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(1..20).each do |i| puts \"#{i} : #{champ i} :: #{champ2 i}\" end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 92,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "180 : 9 :: [2, 190, 10, 0, 9]\n",
+ "181 : 5 :: [2, 190, 10, 1, 5]\n",
+ "182 : 9 :: [2, 190, 10, 0, 9]\n",
+ "183 : 6 :: [2, 190, 10, 1, 6]\n",
+ "184 : 9 :: [2, 190, 10, 0, 9]\n",
+ "185 : 7 :: [2, 190, 10, 1, 7]\n",
+ "186 : 9 :: [2, 190, 10, 0, 9]\n",
+ "187 : 8 :: [2, 190, 10, 1, 8]\n",
+ "188 : 9 :: [2, 190, 10, 0, 9]\n",
+ "189 : 9 :: [2, 190, 10, 1, 9]\n",
+ "190 : 1 :: [3, 2890, 190, 0, 1]\n",
+ "191 : 0 :: [3, 2890, 190, 1, 0]\n",
+ "192 : 0 :: [3, 2890, 190, 2, 0]\n",
+ "193 : 1 :: [3, 2890, 190, 0, 1]\n",
+ "194 : 0 :: [3, 2890, 190, 1, 0]\n",
+ "195 : 1 :: [3, 2890, 190, 2, 1]\n",
+ "196 : 1 :: [3, 2890, 190, 0, 1]\n",
+ "197 : 0 :: [3, 2890, 190, 1, 0]\n",
+ "198 : 2 :: [3, 2890, 190, 2, 2]\n",
+ "199 : 1 :: [3, 2890, 190, 0, 1]\n",
+ "200 : 0 :: [3, 2890, 190, 1, 0]\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "180..200"
+ ]
+ },
+ "execution_count": 92,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(180..200).each do |i| puts \"#{i} : #{champ i} :: #{champ2 i}\" end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 93,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "2880 : 6 :: [3, 2890, 190, 2, 6]\n",
+ "2881 : 9 :: [3, 2890, 190, 0, 9]\n",
+ "2882 : 9 :: [3, 2890, 190, 1, 9]\n",
+ "2883 : 7 :: [3, 2890, 190, 2, 7]\n",
+ "2884 : 9 :: [3, 2890, 190, 0, 9]\n",
+ "2885 : 9 :: [3, 2890, 190, 1, 9]\n",
+ "2886 : 8 :: [3, 2890, 190, 2, 8]\n",
+ "2887 : 9 :: [3, 2890, 190, 0, 9]\n",
+ "2888 : 9 :: [3, 2890, 190, 1, 9]\n",
+ "2889 : 9 :: [3, 2890, 190, 2, 9]\n",
+ "2890 : 1 :: [4, 38890, 2890, 0, 1]\n",
+ "2891 : 0 :: [4, 38890, 2890, 1, 0]\n",
+ "2892 : 0 :: [4, 38890, 2890, 2, 0]\n",
+ "2893 : 0 :: [4, 38890, 2890, 3, 0]\n",
+ "2894 : 1 :: [4, 38890, 2890, 0, 1]\n",
+ "2895 : 0 :: [4, 38890, 2890, 1, 0]\n",
+ "2896 : 0 :: [4, 38890, 2890, 2, 0]\n",
+ "2897 : 1 :: [4, 38890, 2890, 3, 1]\n",
+ "2898 : 1 :: [4, 38890, 2890, 0, 1]\n",
+ "2899 : 0 :: [4, 38890, 2890, 1, 0]\n",
+ "2900 : 0 :: [4, 38890, 2890, 2, 0]\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "2880..2900"
+ ]
+ },
+ "execution_count": 93,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(2880..2900).each do |i| puts \"#{i} : #{champ i} :: #{champ2 i}\" end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 96,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 96,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(1..38889).all? {|i| champ2(i) == ch[i]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 99,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "210"
+ ]
+ },
+ "execution_count": 99,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(0..6).map {|e| champ2(10**e)}.reduce(:*)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Ruby 2.4.0",
+ "language": "ruby",
+ "name": "ruby"
+ },
+ "language_info": {
+ "file_extension": ".rb",
+ "mimetype": "application/x-ruby",
+ "name": "ruby",
+ "version": "2.4.0"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}