Ramanujan might take a little longer on this one

This problem is a much more difficult computer-science follow-up to this question . If you have already read the paragraph about Ramanujan there, you can skip it here.

Today, Sunday, December 22 22 , is Srinivasa Ramanujan 's 126 th 126\text{th} birthday. Ramanujan was an amazing mathematician, but one of the things he was most famous for had to do with the number 1729 1729 . When Ramanujan was in the hospital, he was visited by his friend G.H. Hardy. Hardy remarked that the taxicab that he had ridden in had a rather uninteresting number: 1729 1729 . Ramanujan said that no, 1729 1729 was very interesting because it was the smallest number that can be expressed as the sum of 2 2 cubes in 2 2 different ways. These are 1 2 3 + 1 3 12^3+1^3 and 1 0 3 + 9 3 10^3+9^3 . Hence, numbers that can be written as the sum of multiple cubes in more than 1 1 way are called taxicab numbers. Some more really interesting information about taxicab numbers can be found in this video .

So here's the problem.

Let f ( n ) f(n) be a function that finds the sum of the digits of n n . What are the last 3 3 digits of this expression? i = 1 17 j = 1 29 f ( i 3 + j 3 ) \sum_{i=1}^{17}\sum_{j=1}^{29} f(i^3+j^3)


The answer is 289.

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.

25 solutions

Daniel Chiu
Dec 22, 2013

We simply bash it out.

Python:

def f(x):
    return sum([int(x) for x in list(str(x))])

total = 0
for i in range(1,18):
    for j in range(1,30):
        total+=f(i**3+j**3)
print total

First, I write a function that calculates f ( x ) f(x) (only one line!). Then, I loop over i i and j j and find the total, which is 8 289 8\boxed{289} .

Pebrudal Zanu
Dec 23, 2013

Excel Solution:

Cell A 1 A_1 result from sumation(We can solve by excel too)

Cell B 1 B_1 using formula =floor(A1/10000;1)

Cell C 1 C_1 using formula =floor((A1-10000*B1)/1000;1)

Cell D 1 D_1 using formula =floor((A1-10000 B1-1000 C1)/100;1)

Cell E 1 E_1 using formula =floor((A1-10000 B1-1000 C1-100*D1)/10;1)

Cell F 1 F_1 using formula =floor((A1-10000 B1-1000 C1-100 D1-10 E1)/10;1)

Cell G 1 G_1 using formula =sum(A1:F1)

Drag A1-G1 to A493-G493

Cell 494 using formula sum(G1:G493)

Result (8289)

So, 289 \fbox{289}

Edit Cell G1: =sum(B1: G1) Drag B1-G1 to B493-G493

pebrudal zanu - 7 years, 5 months ago
Priyanka Banerjee
Dec 23, 2013

Java Code ::::

import java.io.*;

import java.math.*;

public class RamanujanCanDoThisInstantly

{

public static void main(String[] args)throws IOException

{

    RamanujanCanDoThisInstantly obj=new RamanujanCanDoThisInstantly();

    long h=0,j=0,i=0,k=0;

    for(i=1;i<18;i++)

    {

        for(j=1;j<30;j++)

        {

            h=(i*i*i)+(j*j*j);

            k=k+obj.SG(h);

        }

    }

    System.out.println(k);

}

long SG(long c)

{

    long j=0;

    while(c!=0)

    {

        j=j+(c%10);

        c/=10;

    }

    return j;

}

}

Output ::: 8289

Noah Singer
Dec 23, 2013

First, we set up a variable answer that will hold our final answer:

answer = 0

We know that sigma notation is inclusive, i.e. that i = k n \sum_{i=k}^n can be represented in a Python for loop with bounds k and n+1, i.e.

for i in range(k,n+1):

We already have our two loops; i i is in [ 1 , 17 ] [1,17] and j j is in [ 1 , 29 ] [1,29] . We can therefore write a nested loop like this:

for i in range(1, 18):
  for j in range(1, 30):

We then just need to find a way to represent the sum of the digits of a number. This can be achieved by using a clever method found here on StackOverflow . Basically, the map function in python applies a function to every element of an iterable. We find the value of i 3 + j 3 i^3+j^3 , convert it to a string, use map and int to get an array of numbers, and then use sum to add it up, like so:

answer += sum(map(int, str(i**3 + j**3))))

Combining the code, the final program is:

answer = 0
for i range(1, 18):
  for j in range(1, 30):
    answer += sum(map(int, str(i**3 + j**3)))
Lokesh Sharma
Dec 23, 2013

Language used: Python

def digitSum(n):
    '''
    Return the sum of digits of number n
    '''
    return sum(map(int, str(n)))

res = 0
for a in range(1, 18):
    for b in range(1, 30):
        res += digitSum(a**3 + b**3)
print res

Alternate solution with PARI: f(x)=sum(k=1,500,floor(x/(10^(k-1)))-10*floor(x/(10^k))); sum(i=1,17,sum(j=1,29,f(i^3+j^3)))

Edward Jiang - 7 years, 5 months ago

How to you format in code like that?

Edward Jiang - 7 years, 5 months ago

Log in to reply

Indent every line to 4 spaces.

Lokesh Sharma - 7 years, 5 months ago
Akshaj Kadaveru
Dec 25, 2013

public class Main { public static void main (String[] args) { int ans = 0; for(int i=1; i<=17; i++) for(int j=1; j<=29; j++) ans += sumDigits(i i i + j j j); System.out.println(ans); } public static int sumDigits(int n) { char[] digits = String.valueOf(n).toCharArray(); int sum = 0; for(int i=0; i<digits.length; i++) sum += Integer.parseInt(Character.toString(digits[i])); return sum; } } The above program outputs 8289 \texttt{8289} , so our answer is 289 \boxed{289}

!gnittamrof eciN

Ahaan Rungta - 7 years, 5 months ago

Log in to reply

Agreed! \texttt{Agreed!}

Sreejato Bhattacharya - 7 years, 5 months ago
Michael Thornton
Dec 24, 2013

To solve this problem I used two iterating structures to compute the values of i i and j j for i = 1 17 \sum_{i = 1}^{17} and j = 1 29 \sum_{j = 1}^{29} . I also devoted a section of code that reads i 3 + j 3 i^3 + j^3 into a string, iterates through the string and adds all the digits to a global variable count . I'll submit the python 3 code beneath but it's a bit ugly - I want to develop my python skills but C++ is really where I like to be; unfortunately this wasn't available this time.

count = 0

#define global variable

for i in range(1, 18):
    for j in range(1, 30):

    #these lines iterate through 1, 17
    #and 1, 29 to get the values of i and j

        lis = str((i**3 + j**3))
        for x in range(len(lis)):
            count+= int(lis[x])

                    #this section reads i^3 + j^3 into a string, and
                    #iterates through the list, adding each value
                     #to count
print(count)
8289
>>>

The answer is 289 \boxed{289}

Aman Tiwari
Dec 24, 2013

class k

{

void main()

{

int sf=0;

for(int i=1;i<=17;i++)

{ int sj=0;

for(int j=1;j<=29;j++)

{int s=0;

int n=i i i + j j j;

(while(n>0)

{

int d=n%10;

s=s+d;

n=n/10;

}

sj=sj+s;

}

sf=sf+sj;

}

System.out.println(sf);

}

}

Ahaan Rungta
Dec 24, 2013

This Python Code prints 8289 , so the answer is 289 \boxed {289} .

def f(n): 
  def sum_digits(n):
    s = 0
    while n:
        s += n % 10
        n /= 10
    return s
  return sum_digits(n) % 1000

ans = 0
for i in range (1, 18): 
  for j in range (1, 30): 
    ans += f(i**3+j**3) % 1000

print ans

and here's a c programme

include<stdio.h>

int main() { int i, j, s, k=0, sum;

for(i=1; i<18; i++)
{
    for(j=1; j<30; j++)
    {
        s=i*i*i + j*j*j;
        sum=0;
        while(s>0)
        {
            sum=sum + s%10;
            s=s/10;

        }
        k=k+sum;
    }
}
printf("%d\n", k);
return 0;

}

Here is c++ code. It is self-explanatory.

#include <iostream>
using namespace std;

int digitSum (int n) {
    if (n == 0) return 0;
    int d = n % 10;
    return d + digitSum((n-d)/10);
}

int main () {
    int total = 0;
    for (int i = 1; i <= 17; i++) {
        for (int j = 1; j <= 29; j++) {
            total += digitSum(i*i*i+j*j*j);
        }
    }
    cout << total << '\n';
    return 0;
}

Harshal Sheth - 7 years, 3 months ago
Michael Tang
Dec 23, 2013

Here's a solution in Java:

public class Ramanujan {
    public static int sumOfDigits(int n)
    {
        int sum = 0;
        String nstr = Integer.toString(n);
        // Converts to string

        for (int i=0; i<nstr.length(); i++)
            sum += Integer.parseInt(Character.toString(nstr.charAt(i)));
            // Adds each digit to the sum (converted to int)

        return sum;
    }

    public static void main(String[] args)
    {
        int sum = 0;
        for (int i=1; i<=17; i++)
            for (int j=1; j<=29; j++)
                sum += sumOfDigits(i*i*i + j*j*j); 
                // Nests the loops
        System.out.println(sum);
    }
}

I did it this way as well.

David Kroell - 7 years, 5 months ago
Tarun Boddupalli
Jul 18, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
function f(n) {
  var sum = 0;
  n.toString().split("").forEach(
  function(x) {
    sum += parseInt(x);
  });
  return sum;
}

var sum = 0;

for (var i = 1; i <= 17; i++) {
  for (var j = 1; j <= 29; j++) {
    sum += f(i*i*i + j*j*j);
  }
}

console.log(sum); // prints "8289"

As you can see, 289 are the last three digits of the result.

Mark Anastos
May 21, 2016

In Haskell:

import Data.Char (digitToInt)
ans = (`mod` 1000) $ sum [ sumOfDigits $ i ^ 3 + j ^ 3 | i <- [1 .. 17], j <- [1 .. 29] ]
    where sumOfDigits = sum . map digitToInt . show

Python:

Abu Sayem
Feb 4, 2015

include <bits/stdc++.h>

using namespace std;

int main() { long long i,j,sum=0,k;

for(i=1;i<18;i++)

{
    for(j=1;j<=29;j++)
    {
    long long d=0;
        k=(i*i*i)+(j*j*j);
        while(k>0)
        {
            d=d+k%10;
            k=k/10;
        }
        sum=sum+d;
    }
}
cout<<sum%1000;

}

Connor Kenway
Apr 10, 2014

include <iostream>

#include<cmath>
using namespace std;

int main() 
{
    int count=0;
    for(int i=1;i<=17;i++)
    {
        for(int j=1;j<=29;j++)
        {
            int sum=((i*i*i)+(j*j*j));
            int tot=0;
            while(sum)
            {
                tot+=(sum%10);
                sum=sum/10;
            }
            count+=tot;
        }
    }
    cout<<count;
}
Samarth Bhargav
Feb 28, 2014

My Solution:

def f(num):
     s = str(num)
    dsum = 0
    for i in s:
        dsum += int(i)
    return dsum

s = 0
for i in range(1,18):
    for j in range(1,30):
        cubed = i**3 + j**3
        s += f(cubed)

print str(s)[-3:]
Adrian Duong
Jan 28, 2014

Code:

sum_digits = lambda n: sum(map(int, str(n)))
sum(sum_digits(i**3 + j**3) for i in range(1, 17 + 1) for j in range(1, 29 + 1))
Jubayer Nirjhor
Jan 22, 2014

Python Interpretation

def f(n):
    val = 0
    while n:
        val += n % 10
        n /= 10
    return val

Answer = 0

for i in range (1, 18):
    for j in range (1, 30):
        inp = (i ** 3) + (j ** 3)
        Answer += f(inp)

print Answer % 1000

Output: 289 \fbox{289} .

Rakha Kautsar
Jan 9, 2014

Assume all necessary includes is done, this is the code in C++:

int f(int n){
    int ret=0;
    while(n){
        ret+=n%10;
        n/=10;
    }
    return ret;
}

int main(int argc, char **argv){
    ios_base::sync_with_stdio(0);
    cin.tie(0); 

    int sum=0;
    for(int i=1;i<=17;++i)
        for(int j=1;j<=29;++j){
            sum=(sum+f(i*i*i+j*j*j))%1000;
        }

    cout<<sum<<'\n';

    return 0;
}

Here is a recursive way to implement f():

int f (int n) {
    if (n == 0) return 0;
    int d = n % 10;
    return d + digitSum((n-d)/10);
}

Harshal Sheth - 7 years, 3 months ago
Ali Ismaeel
Dec 28, 2013

an answer written in C++ ...

we will deal with variable of the type long int just to be sure

we first need to write a function which gives f(n)

long int f=(long int n){  //definition of the function
long int sum=0;         //initial value for the variable sum which accumulates digits of n
while (n/10 > 0){        //as long as we still have more than one digit
    sum+=n%10;          //add the first digit on the right to the sum
    n/=10;}             //discard the first digit on the right
sum+=n%10;              //add the remaining digit
return sum;             //return the value calculated

then we're going to write a piece of code which calculates the inner sum

j = 1 29 f ( i 3 + j 3 ) \sum_{j=1}^{29} f(i^{3}+j^{3})

long int GT1=0;
for (int j=1;j<=29;j++)
    GT1+=f((i*i*i) + (j*j*j));

now GT1 has the value of the inner sum at a certain value of i

we integrate this piece of code with another code which calculates the desired answer to the problem

GT1=0;  GT2=0;      //giving initial value of 0
for (int i=1;i<=17;i++)
    for (int j=1;j<=29;j++){
        GT1+=f(i*i*i+j*j*j);
    GT2+=GT1;
    GT1=0;}     //resetting GT1 befor calculating another term of the outer sum

the complete program is the following

#include <iostream>
using namespace std;

long int f(long int n){
    long int sum=0;
    while (n/10 > 0){
        sum+=n%10;
        n/=10;}
    sum+=n%10;
    return sum;
}

void main(){
    long int GT2=0,GT1=0;
    for (int i=1;i<=17;i++)
        for (int j=1;j<=29;j++){
            GT1+=f(i*i*i+j*j*j);
        GT2+=GT1;
        GT1=0;}
    cout<<GT2%1000<<endl;
}

sorry for formatting mistakes...couldn't correct them

Ali Ismaeel - 7 years, 5 months ago
William Zhang
Dec 26, 2013

Python

def sum(n):

        s=0

        if (n>9):

            while (n>9):

                r=n%10

                n=n//10

                s=s+r

        s=s+n

        return(s)

    a=0

    for i in range(1, 30):

        for j in range(1, 18):

            a=a+sum(i**3+j**3)

    print(a)
Haroun Meghaichi
Dec 24, 2013

Using Mathematica :

Mod[Sum[Sum[Total[IntegerDigits[m^3 + n^3]], {n, 1, 29}], {m, 1, 17}], 1000]

Adel Ali
Dec 23, 2013

We can make 2 nested loops that sums the values of the function for each i 3 i^3 and j 3 j^3 , then we take the ans % 1000 since we need only the last 3 digits, pad the final answer with zeros.

My solution is written in c++ http://ideone.com/u9qo8r

Carl Denton
Dec 23, 2013

I solved this in java. I created a method f(n) specified by:

public static int f(int n)
{
    int sum = 0;
    String s = Integer.toString(n);
    char[] digits = s.toCharArray();
    for (int i=0;i<digits.length;i++)
        {
            sum += Integer.parseInt("" + digits[i]); 
        }
    return sum; 
}

The main method then consisted of:

public static void main(String[] args)
{
    int r = 0;
    for(int i=1;i<=17; i++)
    {
        for(int j=1;j<=29;j++)
        {
            r += f(i*i*i+j*j*j);
        }
    }
    System.out.println(r);
}

Is there no one here who can solve this problem without using programming techniques?? can anyone solve this simply by using mathematics??

kirtan bhatt - 7 years, 5 months ago

Log in to reply

I don't think so. The function f f defined here does not have a particularly nice mathematical form.

Ahaan Rungta - 7 years, 5 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...