Test case running when provided in custom input, but not in submission

I am facing the same issue again. while providing the test case as custom input code is working fine. but after submitting the code test case is not passing… ?

ip : 99999979

op : 4999997900000220 4999997900000221

my code:
import java.util.*;

public class Main {

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	long num = sc.nextLong();
	if(num<=2) System.out.println("-1");
	else{
		if(num%2==0){
			long var = (long) Math.pow(num,2)/4;
			System.out.println((var-1)+" "+(var+1));
		}else{
			long n1 = (long)(Math.pow(num,2)-1)/2;
			long n2 = (long)(Math.pow(num,2)+1)/2;
			System.out.println(n1+" "+n2);
		}
	}
}

}

//had the same issue for “basic calculator” problem

@adarshkumar4145,
This time your code has a mistake.

https://ide.codingblocks.com/s/233563 corrected code.

Don’t use pow instead just multiply the numbers like n*n instead of Math.pow(n,2)

The code with Math.pow() is working fine when custom input is provided(online) which means code is correct and even online compiler is considering math library. consider the cases when even higher powers are needed or square of complex equation is needed. However I really appreciate your quick effort to resolve the issue. thanks for suggestion. regards.

@adarshkumar4145,

pow(x, 2) as you write in your code would be x^2 which you could compute more easily as x*x .

The reason of the error message is the fact that the result of Math.pow is double, and implicit conversion from double to long is forbidden in Java. You’d have to write an explicit cast, i.e. (long)pow(2, x) . But that does round towards zero, so a slight numeric error might cause a wrong result.

According to the documentation Math.pow will promote both of its arguments to double and return double. Obviously when the returned result is double and you cast it to long you’ll get only the highest 64 bits and the rest will be truncated.

Ohh. nice explanation. thank you so much :slight_smile: