Question

In: Computer Science

C program //In this assignment, we will find the smallest positive integer that // can be...

C program

//In this assignment, we will find the smallest positive integer that
// can be expressed as a sum of two positive cube numbers in two distinct ways.
// More specifically, we want to find the smallest n such that n == i1*i1*i1 + j1*j1*j1,
// n == i2*i2*i2 + j2*j2*j2, and (i1, j1) and (i2, j2) are not the same in the sense that
// not only (i1, j1) not euqal to (i2, j2), but also (i1, j1) not equal to (j2, i2).
// We practice using loops in this assignment.
// Also, we practice how to do an early exit in a loop. We exit the loop once we find the smallest n.
// Once we are done, we will be delighted surprised to find out that this number already played a big role in
// our computer science study.
#include <stdio.h>
int main()
{
   int i, j, n;

   //We assume the smallest such n is no more than 1000000
   for(n=1; n<=1000000; n++)
   {
       //Use count to record the number of different ways of summing two positive cube numbers to n
       int count = 0;
       //TO DO
       //Fill in code below

   }


   //Do not change code below
printf("%d is the solution:\n", n);
   for(i=1; i<=100; i++)
       for(j = i; j<= 100; j++)
           {
               if (i*i*i + j*j*j == n)
               {
                   printf("%d * %d * %d + %d * %d * %d = %d\n", i, i, i, j, j, j, n);
}
           }

   return 0;
   //Do not try to hard code the solution, that is not the way to learn.
}

Solutions

Expert Solution

Here is the code:

#include <stdio.h>
#include <math.h>


int main()
{
int i, j, n;

//We assume the smallest such n is no more than 1000000
for( n=1; n<=1000000; n++)
{
//Use count to record the number of different ways of summing two positive cube numbers to n
int count = 0;

  // find cube root of n using cbrt

   for (int i = 1; i <= cbrt(n); i++)
for (int j = i + 1; j <= cbrt(n); j++)
if (i*i*i + j*j*j == n)
count++;

if (count == 2)
  
break;
  


}
     
  

//Do not change code below
printf("%d is the solution:\n", n);
for(i=1; i<=100; i++)
{
for(j = i; j<= 100; j++)
{
if (i*i*i + j*j*j == n)
{
printf("%d * %d * %d + %d * %d * %d = %d\n", i, i, i, j, j, j, n);
}
} }

return 0;
//Do not try to hard code the solution, that is not the way to learn.
}

OUTPUT:

1729 is the solution:
1 * 1 * 1 + 12 * 12 * 12 = 1729
9 * 9 * 9 + 10 * 10 * 10 = 1729

Logic is:

If a number X has pairs like (a,b) and (c,d) then as X<N, all the numbers a,b,c,d must be less than cube root of N


Related Solutions

Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
Find the smallest positive integer x that satisfies the system of congruences x ≡ 3 (mod...
Find the smallest positive integer x that satisfies the system of congruences x ≡ 3 (mod 5). x ≡ 5 (mod 7). x ≡ 7 (mod 11)
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
The least common multiple of nonzero integers a and b is the smallest positive integer m...
The least common multiple of nonzero integers a and b is the smallest positive integer m such that a|m and b|m. It is denoted [a, b], or sometimes [a, b] for short. Prove the following: 1) If a|k and b|k, then [a, b]|k. 2) If gcd(a, b) = 1, then [a, b] =ab 3) If c >0,then [ca, cb] =c·[a, b]. 4) If a >0 and b >0,then [a, b] =ab / gcd(a, b).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT