So, here is my requirement. If a quadratic equation has two roots(an int & a float), I want to take only integer value for further manipulation. I can't figure it out how it's made. Can anyone tell me please. (Java would be better).
-
1what have you tried and what are you having trouble with? There are many ways you could test if a number is a whole number. I like `x == (long) x` or `x == Math.round(x)` – Peter Lawrey Aug 29 '18 at 08:07
-
This is the question I was asked.Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3. You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build? The parameter of the function findNb (find_nb, find-nb, findNb) will be an integer m and you have to return the integer n such as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n. – Harsha Bharadwaz Aug 29 '18 at 08:10
-
1Always share your code while asking. This seems to be home work to me. Anyways i will give you a hint just check whether the integer part of the number is equal to number..if it is then it is a whole number otherwise a floating number – Sid Aug 29 '18 at 08:10
-
Thank you for giving the hint – Harsha Bharadwaz Aug 29 '18 at 08:12
-
1I would use a loop to sum 1^3 + 2^3 etc until you have a matching value or one too large. You don't need to solve roots or use float. – Peter Lawrey Aug 29 '18 at 08:14
-
There is no quadratic equation in the problem statement or immediately apparent in solving it. – Eric Postpischil Aug 29 '18 at 11:24
2 Answers
[I do not use Java regularly. Here is a solution using C. As only elementary concepts are used, a Java practitioner should be able to translate it readily.]
Searching the web for “sum of cubes” reveals this page which tells us the sum of k3 for k from 1 to n is n2•(n+1)2/4.
That is a quartic equation, for which closed-form solutions are known, but we easily see that, for positive n, n2•(n+1)2/4 is between n4/4 and (n+1)4/4. Then, if m is the sum of the first n cubes, n = floor((4•m)1/4). So, if we have a pow
implementation that is faithfully rounded using round-to-nearest (the computed result is one of the two representable values nearest the mathematical result), we can find n with floor(pow(4*m, .25))
. If pow
is not faithfully rounded, then round(pow(4*m, .25))
will serve over the domain for which pow
returns some reasonable result without too much error. (round
works because (4•m)1/4 never exceeds n by more than ½. Proof omitted, although Wolfram Alpha shows us the limit as n goes to ∞ is ½, and the excess is monotonic.)
Thus, if m is the sum of the first n cubes, then n is the result of round(pow(4*m, .25))
. So we can compute this value for n, then compute the sum of the first n cubes as n*n*(n+1)*(n+1)/4
and test whether that equals m. If it does, we found a solution and return it. If it does not, m is not a sum of cubes, and we return −1:
#include <math.h>
#include <stdio.h>
static double findNb(double m)
{
double n = round(pow(4*m, .25));
double sum = n * n * (n+1) * (n+1) / 4;
return m == sum ? n : -1;
}
static void Test(double m)
{
printf("findNb(%.99g) -> %.99g.\n", m, findNb(m));
}
int main(void)
{
Test(0);
Test(1);
Test(2);
Test(8);
Test(9);
Test(10);
Test(250500249999.);
Test(250500250000.);
Test(250500250001.);
}
Output:
findNb(0) -> 0. findNb(1) -> 1. findNb(2) -> -1. findNb(8) -> -1. findNb(9) -> 2. findNb(10) -> -1. findNb(250500249999) -> -1. findNb(250500250000) -> 1000. findNb(250500250001) -> -1.
Of course, the limits of floating-point precision will cause this code to fail once m is larger than can be represented in double
.

- 195,579
- 13
- 168
- 312
Use the basic quadatic formula to find the roots.
Set the roots to different values (both doubles)
Use modulous (%) by 1 and cast the value to a double. If the double calculated is !=0, then it is not an int.

- 134
- 1
- 10