Why 1534236469 can't pass [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I think 1534236469 out of range! Leetcode: 7. Reverse Integer
I can't pass the test input 1534236469. why? the return range is[Integer.MAX_VALUE, Integer.MIN_VALUE],others should return zero
class Solution {
public int reverse(int x) {
if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE)
return 0;
int ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
return ans;
}
}
Thank for your help

The reverse of 1534236469 is 9646324351, which is larger than Integer.MAX_VALUE, so your code will result in numeric overflow and an incorrect result.
You can use long instead of int to fix the problem.
EDIT:
Your added if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) condition is pointless, since x is int, so it will never be outside the valid range of ints.
Even if x is within the valid range, the reverse of x may be outside the range. If you want to detect that the reversed x is too large and return 0, you should use long internally:
class Solution {
public int reverse(int x) {
long ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) {
return 0;
} else {
return (int) ans;
}
}
}

Related

Exception in thread “main” java.lang.ArithmeticException: / by zero [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
My program is supposed to print all numbers from 1111-9999 where all numbers are evenly divisible by n. However, I am getting an exception stating that I'm dividing by zero. Why is this?
package NestedLoops;
import java.util.Scanner;
public class SpecialNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 1111; i < 9999; i++) {
int firstdigit = i % 10;
int seconddigit = (i / 10) % 10;
int thirddigit = (i / 100) % 10;
int fourtdigit = i / 1000;
if ((n % firstdigit == 0)&& (n % seconddigit == 0) && (n % thirddigit == 0) && (n %
fourtdigit == 0)) {
System.out.printf("%d ", i);
}
}
}
}
enter image description here
This is happens because the % operator are using dividing to calculate the result, so anyNumber % 0 will throw an java.lang.ArithmeticException: / by zero.
To prevent this, check if your digits are equal to 0 before the if statement.

Finding whether number 4 is prime or not? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here is my program, inside for loop Math.sqrt(n) - I think here is error.
import java.util.Scanner;
public class PrimeNoCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
if (isPrime(n)) {
System.out.println(n + " is a prime number");
} else {
System.out.println(n + " is not a prime number");
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
the algorithm is:
To check if n is a prime number, you have to try to divide it by all the numbers, from 2 (1 is useless), to sqrt(n), included.
You don't have to check after sqrt(n), because, if n can be divided by X,
greater than sqrt(n), it also can be divided by Y (n/X), lesser than sqrt(n), or by Z, lesser than Y, if Y is not prime.
The error in you code is that you stop just before sqrt(n).
With your code 4, 9, 25, ... are seen as prime, and every prime ^2 also.
just Replace that: <= instead of <
for (int i = 2; i <= Math.sqrt(n); i++) {

How can I check for prime numbers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the simplest way to do this, preferably without lists or maps?
I've tried turning the wikipedia pseudocode into real code but I don't understand the second half after the new line at all.
function is_prime(n : integer)
if n ≤ 1
return false
else if n ≤ 3
return true
else if n mod 2 = 0 or n mod 3 = 0
return false
let i ← 5
while i×i ≤ n
if n mod i = 0 or n mod (i + 2) = 0
return false
i ← i + 6
return true
First off, here's that function converted to java:
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
} else if (n <= 3) {
return true;
} else if (n % 2 == 0 || n % 3 == 0) {
return false;
}
int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
i += 6;
}
return true;
}
The second half is the part that iterates from the lowest unchecked number (5, because multiples of 2 are already checked, as are 0, 1, and 3) to the square root of n (anything greater than the square root would have to be multiplied by something less than the square root to equal n), checking each number i to see if n is evenly divisible by i.
try this code
public static boolean isPrime(int n) {
for (int i = 2; i < n - 1; i++) {
if(n%i==0)
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(39));
}

Dividing double by 0 returns infinty [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The code basically gives me infinty and the sting undefined. I only need undefined. I understand why its giving infinity but dont understand how to get rid of it. I cant use an exception as the error should only print undefined and nothing else.
double divide;
double a;
double b = 0;
if(args.length > 3){
System.out.println("Argument count mismatch");
}
else if(args.length == 1){
System.out.println("Argument count mismatch");
}
else{
a = Integer.parseInt(args[1]);
b = Integer.parseInt(args[2]);
divide = a / b;
System.out.println(divide);
if (b == 0) {
System.out.println("Undefined");
}
}
You can test the result with Double.isInfinite(double) (and you might also want to use Double.isNaN(double) and something like,
double a = 10;
double b = 0;
double divide = a / b;
if (Double.isInfinite(divide) || Double.isNaN(divide)) {
System.out.println("Undefined");
} else {
System.out.println(divide);
}
Output is
Undefined
Another way in addition to Elliott Frisch's way is to check for positive or negative infinity using the constants in the Double class.
if (divide == Double.POSITIVE_INFINITY || divide == Double.NEGATIVE_INFINITY)
{
System.out.println("Undefined");
}

Adding two numbers of large length using strings [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can't seem to figure out what wrong with this method used to add two strings. It has a buffer when stores each added digit and finally displays. However when two huge numbers (greater than 10 digits) are added it just truncates and doesn't store the remainder of the string.
public static void add(String a,String b){
StringBuilder buf = new StringBuilder();
if((a.length() - 1==0)&&(a.charAt(0)=='0')){
System.out.println(b);
return;
}
if((b.length() - 1==0)&&(b.charAt(0)=='0')){
System.out.println(a);
return;
}
else{
for ( int i1 = a.length() - 1, i2 = b.length() - 1, carry = 0;
(i1 >= 0 && i2 >= 0) || carry != 0;i1--, i2-- ) {
int digit1 = i1 < 0 ? 0 :
Integer.parseInt(Character.toString(a.charAt(i1)));
int digit2 = i2 < 0 ? 0 :
Integer.parseInt(Character.toString(b.charAt(i2)));
int digit = digit1 + digit2 + carry;
if (digit > 9) {
carry = 1;
digit -= 10;
} else {
carry = 0;
}
buf.append(digit);
}
}
System.out.println(buf.reverse().toString());
}
Since need to stop when both i1 and i2 reach a negative value, the condition should be
i1 >= 0 || i2 >= 0 || carry != 0
rather than
(i1 >= 0 && i2 >= 0) || carry != 0
Demo on ideone.
Just use BigInteger, and your whole method is then three lines long.

Categories

Resources