f ( n ) gives the sum of the cubed digits of some positive integer n . For example, f ( 1 2 3 ) = 1 3 + 2 3 + 3 3 = 3 6 .
If we repeatedly apply this process on each previous result, the following two different behaviors may arise:
Let the limit set be the set of all fixed points and limit cycles in the range of f ( n ) .
Find the sum of all the numbers in the limit set (including the four in pink found above). Note : A coding environment is provided below:
Bonus: Prove that the limit set actually contains finitely many numbers.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
My c code.Successfully run on xcode.However, I miss the point of this problem.I thought the answer is the number of all these numbers, and tried 15,16,17 o(╯□╰)o。
int c[100] = { 0 };
int count = 0;
bool isprintf = false;
void function(int n)
{
int a[100] = { 0 }, b[100] = { 0 };
a[1] = n;
int i;
for (i = 2; i <= 99; i++)
{
while (a[i - 1] != 0)
{
a[i] = a[i] + (a[i - 1] % 10) * (a[i - 1] % 10) * (a[i - 1] % 10);
a[i - 1] = a[i - 1] / 10;
}
b[i] = a[i];
}
int j, k;
for (i = 2; i <= 99; i++)
{
for (j = 1; j < i; j++)
{
if (b[j] == b[i] && (b[j] != 0))
{
for (k = j; k<i; k++)
{
int x;
for(x = 0; x < count; x++)
{
if(c[x] == b[k])
{
isprintf = true;
break;
}
}
if(isprintf == false)
{
printf("%d\n", b[k]);
c[count++] = b[k];
}
isprintf = false;
}
for (k = i; k <= 99; k++)
b[k] = 0;
}
}
}
}
int main()
{ int n;
for (n = 0; n <= 200; n++)
{
function(n);
}
return 0;
}
the output is
1
371
153
133
55
250
370
217
352
160
407
1459
919
244
136
Another observation can help you go for a number smaller than 2916.With the range of 2916, the largest output is given by 1999 which is 2188.
limit of 10000?
The bonus isn't too hard: let f ( x ) be the number obtained by summing the cubes of the digits of x . Now here are two facts:
(1) If x < 1 0 0 0 0 , then f ( x ) < 1 0 0 0 0 (in fact, it's less than 4 ⋅ 9 3 = 2 9 1 6 ).
(2) If x ≥ 1 0 0 0 0 , f ( x ) < x . (Proof: let g = lo g 1 0 ( x ) . Then x has ⌈ g ⌉ digits, so f ( x ) ≤ 9 3 ⌈ g ⌉ , which is less than x = 1 0 g for g ≥ 4 , because e.g. 7 2 9 ⌈ g ⌉ < 7 2 9 ( g + 1 ) , and for g = 4 7 2 9 ( g + 1 ) < 1 0 g and the derivative of the left side is a constant 7 2 9 and the derivative of the right side is 1 0 g ln ( 1 0 ) ≥ 1 0 0 0 0 ln ( 1 0 ) , so the right side increases faster than the left side.)
This implies that for any x , iterating f enough times will leave a result < 1 0 0 0 0 , and iterating f after that will keep us in the range [ 1 , 1 0 0 0 0 ] . By the Pigeonhole Principle , the sequence of iterates will eventually repeat, at which point we are in the limit set.
This shows that everything eventually leads to a point in the limit set, and that the limit set is finite, because no number > 1 0 0 0 0 can ever appear in the limit set.
To generate the limit set, I wrote some code to check the integers < 1 0 0 0 0 . I got the cycles 1 5 5 , 2 5 0 , 1 3 3 1 3 6 , 2 4 4 1 5 3 1 6 0 , 2 1 7 , 3 5 2 3 7 0 3 7 1 4 0 7 9 1 9 , 1 4 5 9 The sum is 5 2 2 7 .
I don't get it im in 6th grade
i Don't agree. f(1459) is 918. so the last set isn't correct. then the overall set sum must be 2849
I don't know how used you are to dealing with such iteration problems, but one thing that I find helpful is to plot the function beforehand to get some intuition. Other than that, your solution is clearly the best one available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Let's call a number made of k 9's a ninber . A ninber has clearly the highest possible output of f ( n ) for a number with k digits. Also, it's easy to check that its output is equal to k × 9 3 . So the output increases linearly with k, as the ninber itself (which consists of k 9's) grows exponentially. So, if we find the least k for which the ninber is bigger than its output, we are guaranteed that greater numbers will always be greater than their outputs, so we don't need to check them as it's impossible for them to form fixed points or limit cycles.
f ( 9 9 9 9 ) = 2 9 1 6 < 9 9 9 9
Bonus : This also proves that the limit set contains finitely many numbers, as numbers greater 2916 can't be part of the set and there are finitely many positive integers smaller or equal to 2916.
Here is a concise example that computes the limit set:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Why turn str(n) into a list in line 7? You can iterate over the string directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
My C code.
include <stdio.h>
int c[100] = { 0 };
int count = 0;
bool isprintf = false;
void function(int n)
{
int a[100] = { 0 }, b[100] = { 0 };
a[1] = n;
int i;
for (i = 2; i <= 99; i++)
{
while (a[i - 1] != 0)
{
a[i] = a[i] + (a[i - 1] % 10) * (a[i - 1] % 10) * (a[i - 1] % 10);
a[i - 1] = a[i - 1] / 10;
}
b[i] = a[i];
}
int j, k;
for (i = 2; i <= 99; i++)
{
for (j = 1; j < i; j++)
{
if (b[j] == b[i] && (b[j] != 0))
{
for (k = j; k<i; k++)
{
int x;
for(x = 0; x < count; x++)
{
if(c[x] == b[k])
{
isprintf = true;
break;
}
}
if(isprintf == false)
{
printf("%d\n", b[k]);
c[count++] = b[k];
}
isprintf = false;
}
for (k = i; k <= 99; k++)
b[k] = 0;
}
}
}
}
int main()
{ int n;
for (n = 0; n <= 200; n++)
{
function(n);
}
return 0;
}
the output is
1
371
153
133
55
250
370
217
352
160
407
1459
919
244
136
the sum is 5227.
I don't get it: f(352) = 160,* so 352 shouldn't be in the set, right? Likewise f(1459) = 919, already in the set. What am I missing here?
*3^3 + 5^3 + 2^3 = 27 + 125 + 8 = 160, which is already in the set.
Log in to reply
The code print numbers in a limit cycle to several lines.
Ex. 217->352->160->217->......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
MAXRANGE = 2195 # 2^3 + 3 * 9**3
UNCHECKED, NOT_IN_LS, TESTING_NOW, BELONGS_LS = 0, 1, 2, 3 # LS - limit set
# Every cell winth index 'i' in the list 'state' represents state of number i
state = [UNCHECKED] * MAXRANGE
def chain_replace(number, source, destination):
"""This function runs throuhg chain which begins with 'number'
and replaces every number state in the chain to 'destination'
while the state is 'source'"""
while state[number] == source:
state[number] = destination
number = sum([int(i)**3 for i in str(number)]) # Sums up cubes of digits
return number
for number in range(MAXRANGE):
cycle_start = chain_replace(number, UNCHECKED, TESTING_NOW)
chain_replace(cycle_start, TESTING_NOW, BELONGS_LS)
chain_replace(number, TESTING_NOW, NOT_IN_LS)
print(sum([i for i, n in enumerate(state) if n==BELONGS_LS]))
Python 3 code:
nums = {} # dictionary of all numbers and what they iterate to
ends = [] # limit set
def f(num): # this is the iterative function, which works in a creative way
sum = 0
for digit in str(num):
sum += int(digit)**3
return(sum)
for num in range(1, 100000): # adds each number up to 99,999 and what it iterates to to the nums dictionary
path = []
iter = num
nums[iter] = f(iter)
while iter not in path:
path.append(iter)
nums[iter] = f(iter)
iter = f(iter)
for maybe in nums: # checks for numbers in the limit set
iter = maybe
cycle = 0
uncompleted = True
while cycle < 20 and uncompleted:
iter = f(iter)
if iter == maybe:
ends.append(maybe)
uncompleted = False
a += 1
sum = 0
for num in ends: # sums all the numbers in the limit set to get to the final sum
sum += num
print(sum) # final sum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
f = Plus @@ Table [ i 3 , { i , IntegerDigits [ $#$1 ] } ] & ;
limitSet = { } ; Do [ limitSet = limitSet ∪ With [ { result = NestWhileList [ f , i , UnsameQ , All ] } , result [ [ Position [ result , Last [ result ] ] [ [ 1 , 1 ] ] + 1 ;; − 1 ] ] ] , { i , 0 , 9 9 9 9 } ] , i ]
Plus @@ limitSet ⟹ 5 2 2 7
I noticed that, we need to iterate only from 1 to 137 to cover all the numbers in limit set. Trying to figure out what is it about '137'...
From 1 to 136 is enough
Python solution:
def sumOfDigitsCubed(n):
result = 0;
while n > 0:
d = n % 10
result += d*d*d
n = n // 10
return result
def findLimitCycle(parm):
tempList = [];
n = parm
while n not in tempList:
tempList.append(n)
n = sumOfDigitsCubed(n)
indx = tempList.index(n)
return tempList[indx:]
result = []
for i in range(1,1000):
thisCycle = findLimitCycle(i)
if thisCycle[0] not in result:
result.extend(thisCycle)
result.sort()
print (result)
print(sum(result))
By the way, how do you format code with line numbers, like I see in other solutions posted here?
You start a line with ‘ ‘ ‘ P y t h o n break line, write your code and end in a new line with ‘ ‘ ‘
The maximum value of f for an n -digit number is 9 3 ⋅ n . Since this is equal to 2 9 1 6 , which has 4 digits, for 4-digit numbers and stays smaller than the numbers as they get larger, only numbers up to 2 9 1 6 need to be considered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Yes, proper Python coders will probably stone me for breaking every principle of the Zen of Python, but it works, so...
Here's a compact solution using dictionaries in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Problem Loading...
Note Loading...
Set Loading...