0

Sigma of Equation Given a number N, find the value of the below equation for the given number.

Equation

N
∑  {(X + 1)^2 - (3X + 1) + X}
X = 1

Input Format

First line contains an integer n

Output Format

Single line output representing the value.

Example 1

Input
1
Output
1

Example 2

Input
2
Output
5

Explanation

{(1+1)² - (3x1+1) + (1)} + {(2+1)² - (3x2+1) +2} = 4 - 4 +1 + 9 -7 +2 = 5

Constraints

1 <= n <= 105

**I have two solution one is working and second is not working for some test cases like n=100000.

Solution 1(Working):**


import java.util.*;
import static java.lang.Math.ceil;

class Solution {
    public long SigmaEquation(int n ) {
        long ans=0;
        for(int i=1; i<=n; i++){
            ans = ans + (long)(Math.pow((i+1),2)-((3*i)+1)+i);
        }
        return ans;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Solution Obj = new Solution();
        System.out.println(Obj.SigmaEquation(n));

    }
}


Solution 2(error for some test cases like n= 100000 ):-


import java.util.*;
import static java.lang.Math.ceil;

class Solution {
    public long SigmaEquation(int n ) {
        long ans=0;
        for(int i=1; i<=n; i++){
            ans = ans + (((i+1)*(i+1))-((3*i)+1)+i);
        }
        return ans;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Solution Obj = new Solution();
        System.out.println(Obj.SigmaEquation(n));

    }
}

Getting this issue for solution 2 why?

and why the same issue solved in solution 1 program?

Wrong Answer

Time Limit : 171ms
Time Taken By Your Code : 152ms

Input

100000

Correct Output

333338333350000

Your Output

18104913692784
azro
  • 53,056
  • 7
  • 34
  • 70

1 Answers1

0

(long)(Math.pow((i+1),2)-((3*i)+1)+i) from your first solution, you are casting the result of your equation as a long which supports very large numbers. in your second you are not casting to a long, so java assumes the result is an int. when the result of that equation starts to become very large, it no longer fits in an int so you are getting unexpected results from the later iterations of your for loop.

Portal
  • 122
  • 7
  • But I alredy define ans as a long data type? So Java assumes result is an int? – vinit yadav Apr 26 '23 at 19:29
  • @vinit yadav It is because all of this part of the equation `(((i+1)*(i+1))-((3*i)+1)+i)` are all integers. If you simply change a single value to long like this -- `(((i+1L)*(i+1))-((3*i)+1)+i)` , note the capital `L` after the `1`, it will now work. – Nexevis Apr 26 '23 at 19:33
  • @vinityadav yes I believe java assumes whole numbers are of type int unless specified otherwise, by setting the answer to a long java is only understanding that this variable should be stored with whatever the space given to long. It does not carry that assumption into the actual calculation of the number – Portal Apr 26 '23 at 19:37
  • so then what you saying is "int + long = int" . Am i right – vinit yadav Apr 26 '23 at 19:50
  • @vinityadav No, in that example it would result in the first number being treated as an int and the second as a long. What I'm saying is if you say `1 + 2` java assumes these numbers are ints. if you cast one to another type like `(long) 1 + 2` now java assumes we are dealing with longs. The real issue here is `int x = 1 + 2` and `long x = 1 + 2` don't change the way the `1` and the `2` are viewed during the calculation. `(long) 1 + 2` does. – Portal Apr 26 '23 at 19:58
  • this behavior is easier to see if you do something like `1 / 2` these two whole numbers are assumed to be ints and so the result is assumed to be an int so java will tell you that equals `0` however if you cast one of the numbers as a double `(double)1 / 2` the answer changes to `0.5` – Portal Apr 26 '23 at 20:02
  • then why java not assume long when I using "ans" variable as long and as we see "equation" as int . In this case why it assume as int and not long – vinit yadav Apr 26 '23 at 20:05
  • java doesn't use context like the variable's type when performing calculations. it simply performs the calculation and then stores whatever the result is within the variable (if possible) as Nexevis mentioned above, if you want java to consider the result of the calculation to be of a certain type then you need to cast that type, or to reuse my previous example, use variables who posses that type `1.0 / 2 = 0.5` – Portal Apr 26 '23 at 20:14
  • Ok, Thank You ....Do you have some study materials about this int ,long ,double, etc... data type relationship concept. It will very help full for me to learn more about this concept . – vinit yadav Apr 26 '23 at 21:13
  • @vinityadav Here is a good section to start: https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2 – Portal Apr 26 '23 at 21:39