Why is my BigInteger Program Not Producing Any Output?

  • Thread starter muna580
  • Start date
  • Tags
    Remainder
In summary, The conversation was about a program that was made to solve a question regarding finding the smallest positive integer x such that x^2 + 3x + 5 is divisible by 121. The program was made using BigInteger and it compiles correctly, but when executed, it does not output an answer. The expert suggests that there is no solution to the question and that using int or long would have been sufficient for this program.
  • #1
muna580
Okay, I made this program in order to solve this question

What is the smallest positive integer x such that x^2 + 3x + 5 is divisible by 121?

The program complies perfectly. But when I execute it, it don't print out any answer.

Code:
import java.math.BigInteger;

public class Number37
{
	public static void main (String[] args)
	{
		BigInteger divider = BigInteger.valueOf(121);
		BigInteger ZERO = BigInteger.ZERO;
		BigInteger THREE = BigInteger.valueOf(3);
		BigInteger FIVE = BigInteger.valueOf(5);

		BigInteger n = BigInteger.valueOf(0);
		
		BigInteger val1 = n.pow(2);
		BigInteger val2 = n.multiply(THREE);

		
		for (long i = 0; i < Long.MAX_VALUE; i++)
		{
			n = BigInteger.valueOf(i);
			
			BigInteger value = (val1.add(val2)).add(FIVE);
			
			if(value.remainder(divider) == ZERO)
			{
				System.out.println("N is equal to: "  + n);
				return;
			}
		}
		
	}
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You don't have to use BigInteger here, you use BigInteger and BigDecimal when
o) need a integer/decimal that is larger than +/- 2^61/2
o) need arbitrary precision that is a double/long can give you only up to 15 digits of precision more or less.

here I think it is safe to use int or long.
 
  • #3
Well, even though I don't need the BigInteger here, I still used it and programmed it correctly. But since its not giving me an answer, would you say there is NO SOLUTION to teh questio nI am trying to program?
 
  • #4
Yes, there is no solution.
 

Related to Why is my BigInteger Program Not Producing Any Output?

1. What is a BigInteger remainder?

A BigInteger remainder is the value that is left over after dividing a BigInteger by another number. It is the equivalent of the modulus operator (%) for smaller data types, but can handle much larger numbers.

2. How do I calculate the remainder of two BigIntegers?

To calculate the remainder of two BigIntegers, you can use the mod method, which is available in most programming languages that support BigIntegers. This method takes in the divisor as a parameter and returns the remainder as a BigInteger.

3. Can a BigInteger remainder be negative?

Yes, a BigInteger remainder can be negative. The sign of the remainder is determined by the sign of the dividend (the first number being divided). If the dividend is positive, the remainder will be positive. If the dividend is negative, the remainder will be negative.

4. What happens if I try to divide by 0 when using BigIntegers?

Trying to divide by 0 when using BigIntegers will result in an ArithmeticException being thrown. This is because division by 0 is undefined and not allowed in mathematics.

5. Are there any limitations to using BigIntegers for calculating remainders?

The main limitation when using BigIntegers for calculating remainders is memory. Since BigIntegers can handle very large numbers, they require more memory than other data types. This can become an issue when dealing with extremely large numbers or performing many calculations in a short amount of time.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
4
Views
23K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
Back
Top