-1

I'm writing a program which must take in an integer, N, in range 3<=N<=10^18. This is one of the operations I have to perform with N.

final=((0.5*(pow(2,0.5))*(pow((pow(((N/2)-0.5),2)+pow((N/2)-0.5,2)),0.5)))-0.5)*4;

N is such that final is guaranteed to contain an integer.

The problem is that I can't store N in a float type as it is too large. If I store it in long long int, the answer is wrong(I think its because the intermediate value of N / 2 is then rounded off).

Christophe
  • 68,716
  • 7
  • 72
  • 138
tnd11
  • 19
  • 5
  • Did you try using `double`s? – Scott Hunter Oct 26 '19 at 11:51
  • I did, but the online judge tells me I got a wrong answer in 2 of the test cases. – tnd11 Oct 26 '19 at 11:53
  • Could you write an alternate version of this for the cases where N is odd, so that you *could* use long longs? (Assuming that is actually the source of the problem.) – Scott Hunter Oct 26 '19 at 11:53
  • Is `long double` available? – Scott Hunter Oct 26 '19 at 11:55
  • see if `unsigned long long int` works. – Arya11 Oct 26 '19 at 11:55
  • @tnd11 *I did, but the online judge tells me I got a wrong answer* -- Note that `pow` is a function that works in floating point. Thus whatever answer you get from that formula may never match what the "online judge" is looking for, since floating point is not exact. Any question from these "online judge" sites that forces you to use floating point in the calculation, my advice is to skip them. – PaulMcKenzie Oct 26 '19 at 11:57
  • 4
    The title of the question seems misleading, since storing 10^18 floats is not the same that storing 10^18 as float. In the first case you'd need `sizeof(float)*10^5` terabytes of RAM and in the second only `sizeof(float)` – Christophe Oct 26 '19 at 12:00
  • 1
    Are the round brackets unbalanced? – Abhishek Agarwal Oct 26 '19 at 12:18
  • @AbhishekAgarwal, they became unbalanced after the question was edited. – Evg Oct 26 '19 at 12:22
  • I've restored the original formula with balanced braces. (Thank you to those editing for nice spacing to verify twice that they do not alter the syntax) – Christophe Oct 26 '19 at 12:36
  • 1
    @tnd11 This seems to be clearly a question on how to avoid floating point by simplifying the expression into something integer-based. It would be highly unfair for an online quiz test to rely on floating point calculations for the answer. – PaulMcKenzie Oct 26 '19 at 12:37
  • @PaulMcKenzie I too think that's the case here. Thanks to all who took their time to contribute to this thread. – tnd11 Oct 26 '19 at 13:54

2 Answers2

2

The correct answer is

final = 2 * abs(N-1) - 2;           

This can be verified, by removing the unnecessary parenthesis, regrouping same terms, distributing multiplication by constants, and using the following identities:

This is almost the same than the accepted answer. But the other answer is correct only for any N >= 1. It is wrong as soon as N-1<0, so in the range of the possible N values allowed by your question, for 0 <= N < 1

You can verify this with this online demo

Edit: following your edit of the question that changes the range and therewith exclude the problematic values, the accepted answer will do. I leave my answer here for the records, and for the sake of the maths ;-)

Christophe
  • 68,716
  • 7
  • 72
  • 138
1

This formula looks intimidating, but can be simplified (if N > 1) to

final = 4 * (N / 2 - 1)

using the identity: (xa)1/a = x.

If N / 2 is supposed to be a floating-point division, not an integer one, the answer is

final = 2 * (N - 2)
Evg
  • 25,259
  • 5
  • 41
  • 83
  • 1
    @rustyx, trivial algebra using the fact that `pow(pow(x, a), 1 / a) = x`. I added this identity into the answer. – Evg Oct 26 '19 at 12:20
  • 1
    Simplifies even more to 2*N-4. – Scott Hunter Oct 26 '19 at 12:23
  • I think that it does not solve the problem for all the cases and in particular for N=0 because the identity is pow(pow(x,2),0,5) = abs(x). – Christophe Oct 26 '19 at 13:04
  • 2
    @Christophe I am terribly sorry for making yet another typo in the original question: N is in the range 3<=N<=10^18 – tnd11 Oct 26 '19 at 13:56