Nothing happens when running my code [BigInteger] - java

So I have a class that computes two prime numbers and then checks if they're prime
import java.util.*;
import java.math.*;
public class GeneratePrime{
public static BigInteger calculatePPrime() {
BigInteger pRandom;
while (true) {
pRandom = new BigInteger(512, new Random());
pRandom = pRandom.setBit(0);
if(isPrime(pRandom)){
System.out.println("Got Random Prime P: "+pRandom);
break;
}
}
return pRandom;
}
public static BigInteger calculateQPrime() {
BigInteger qRandom;
while(true){
qRandom = new BigInteger(512, new Random());
if(isPrime(qRandom)){
System.out.println("Got Random Prime Q: "+qRandom);
break;
}
}
return qRandom;
}
public static boolean isPrime(BigInteger number) {
if (!number.isProbablePrime(5))
return false;
BigInteger two = new BigInteger("2");
if (!two.equals(number) && BigInteger.ZERO.equals(number.mod(two)))
return false;
for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(number) < 1; i = i.add(two)) {
if (BigInteger.ZERO.equals(number.mod(i)))
return false;
}
return true;
}
}
This is more or less a conglomerate of things I have found on the internet. This class was originally not using any BigIntegers, but then I found out I am required to use it for my assignment, so I had to scratch everything and do this.
Anyway. I can't use any built in functions to calculate primes (idk if there even are any). My issue here is that when I run this code in:
import java.util.*;
import java.math.*;
public class RSA{
public static void main(String[] args) {
BigInteger p, q;
GeneratePrime gp = new GeneratePrime();
p = gp.calculatePPrime();
q = gp.calculateQPrime();
}
}
Both classes compile fine, but then when I run the RSA class, nothing happens. No errors no nothing. My terminal is just blank. Does anyone know why? Or can anyone see if this code works on their machine? I know I've probably missed something dumb here. Thanks

You want to get random primes? Then make that clear in your code:
public BigInteger randomPrime(int bits, Random random) {
return new BigInteger(bits, random).nextProbablePrime();
// or
// return BigInteger.probablePrime(bits, random);
}
Use the methods at your disposal, in this case: nextProbablePrime(). This method is so much more efficient than the set of methods you wrote. This will significantly boost your application.
Your RSA class can then become:
import java.util.*;
import java.math.*;
public class RSA{
public static void main(String[] args) {
BigInteger p, q;
GeneratePrime gp = new GeneratePrime();
Random random = new Random();
int bits = 512;
p = gp.randomPrime(bits, random);
q = gp.randomPrime(bits, random);
}
}

If you are willing to use the 512 bits number and you want to randomly get it and hope it will be a prime. You will find no success with your kind of solution. This is just too large value to be handled with too many possibilities. Even if you use the BigInteger constructor with certainty in. Like so :
new BigInteger(int bitLength, int certainty, Random random);
Where the java doc says:
certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/(2certainty)).
The larger you make certainty, the smaller is the probablility that the number is not prime.
Still will not help you. You can consider on some point of taking just the next prime number if the randomed one is not a prime as #Olivier Grégoire suggested or just reduce the number of bits.

The value for maximum bitLength of the new BigInteger is very large. If you reduce it to a smaller number it will work. I'm referring to the value 512 in the code below. Try a small number like 12.
pRandom = new BigInteger(512, new Random());

Related

Got stuck at Project Euler's question 3 i.e. finding the largest prime factor of a given number. Please see details

So I designed this code (given below) for the question & it seems to be giving all the answers there are but for some reason I'm not able to pass my test cases in the Hackerrank Question except for the sample one.
My Code :
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
BigInteger n = new BigInteger(in.next());
BigInteger max = BigInteger.valueOf(1);
for(BigInteger i=BigInteger.valueOf(2);i.multiply(i).compareTo(n)<=0;i=i.add(BigInteger.ONE))
{
if(i.isProbablePrime(1) && n.mod(i).compareTo(BigInteger.valueOf(0))==0 && i.compareTo(max)>=0)
{
max = i;
n = n.divide(i);
}
}
if(n.isProbablePrime(1))
max = n;
else if(n.divide(BigInteger.valueOf(2)).isProbablePrime(1))
max = n.divide(BigInteger.valueOf(2));
System.out.println(max);
}
}
}
I'm getting the same output for each input as this website here.
Can anyone please look have a look at my code & point out where am I going wrong. I'm not looking for new code or logic, I just want to know how & why my code or logic is wrong. Can anyone help?
Thanks in advance.
EDIT : NEW CODE Found The Solution Thanks to #Douglas for helping me with hints
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
long max = 1;
while(n%2==0)
n = n/2;
if(n==1)
max = 2;
for(long i=3;i*i<=n;i+=2)
{
while(n%i==0 && i>=max)
{
max = i;
n = n/i;
}
}
if(n>2)
max = n;
System.out.println(max);
}
}
}
The code above runs perfectly fine & passes all the testcases.
You have three problems with this code:
An actual bug. You don't handle when a prime divides the number multiple times. I'm fairly sure the four lines before the println output are a workaround you put in when you encountered a failure this causes, that fixed the specific failure you found but don't actually address the root cause. I just tested it, your code outputs 3 for the input 252. The correct output for 252 is 7.
Part of your code invokes a library method that may be significantly expensive. You should not be using isProbablePrime.
Your algorithm's logic scales poorly. Hackerrank's test cases probably include something where the correct output is something like 20 digits long. A major point of Project Euler is that it's not enough to write code that will find the correct answer. You need to write code that will find the correct answer efficiently. To fix this, you will need new logic for the fundamental design of the program.

Why isn't this Ackermann function working?

I would expect this to return with 7, given the input of (2,2). Instead of getting a proper output, the program returns a java.lang.StackOverflowError at line 16.
package main;
import java.math.BigInteger;
public class Ackermann {
public static void main(String[] args) {
System.out.println(ack(BigInteger.valueOf(2),BigInteger.valueOf(2)));
}
public static BigInteger ack(BigInteger a, BigInteger b) {
BigInteger ans;
if (a.equals(0)) ans = b.add(BigInteger.ONE);
else if (b.equals(0)) ans = ack(a.subtract(BigInteger.ONE),BigInteger.valueOf(1));
else ans = ack(a.subtract(BigInteger.ONE), ack(a,b.subtract(BigInteger.ONE))); //line 16
return (ans);
}
}
I've increased the maximum stack size all the way up to 2GB, but it's still throwing the error at the small input of (2,2). Before I started using the BigIntegers instead of Longs, everything worked out fine with the input (2,2), but now it's a mess.
Instead of equals(0) you have to use equals(BigInteger.ZERO).
Otherwise you compare a BigInteger to an Integer (auto boxing) which will always be false.

BigInteger loops and logic not working as planned

so I'm trying to make some code where it basically factors really big numbers. I've tried to translate the code that worked using longs into BigIntegers but the result just returns a lot of 2s and 0s. Here it is.
package primes;
import java.math.BigInteger;
public class Primes {
public static void main(String[] args) {
BigInteger y = new BigInteger("0");
BigInteger count= new BigInteger("2");
BigInteger input = new BigInteger("12321");
BigInteger one = new BigInteger("1");
while(input.compareTo(input)!=1) {
y=input.mod(count);
System.out.println(y);
if(y.compareTo(y)==0) {
input=input.divide(count);
System.out.println(count);
} else if(y.compareTo(y)!=0) {
count.add(one);
}
}
}
}
Alright, I see the problem with the count.add(one); but I am still unsure as to how the compareTo function works. Just for clarifciation with what each of these loops are supposed to do, I'm just going to paste the code of the functioning script that works with longs.
package longprimes;
public class LongPrimes {
public static void main(String[] args) {
long input = 121L;
long count = 2;
long y;
while (input!=1){
y = input%count;
if(y==0){
input = input/count;
System.out.println(count);
}
else if(y!=0){
count++;
}
}
}
}
Seemingly what is happening when I updated the BigInteger code a little bit is that it would factor it but it wouldn't divide input so that it would just keep finding the remainder but in the case that it was 0, it didn't actually divide input to end the loop.
Looks like this is a homework assignment, so I'll just point out a few things:
input.compareTo(input)!=1 is probably not what you intended. Ideally, you want to finish that while loop when input is one. But compareTo will return -1, 0 or 1 if the number on the left is less, equal to, or greater than the other number. Consider what this condition actually returns.
y.compareTo(y)==0 is kind of the same thing. You're comparing a number y with itself. Ideally, you want to compare with zero.
count.add(one) is not really doing anything. Consider what happens when you simply do
1 + 2;
you're computing the result of adding one to count but you're also ignoring the result. You might want to save that result somewhere.

Finding really big power of a number

I am creating a small game for students, and in a place, it has to display the value of 27830457+1
I can call BigInteger's pow() method if the number is not this much big. Since the number is very big, that method is useless. How can I find the HUGE power of this kind of numbers? Please help!
Well in binary it's just 10000...01 with 7830456 zeros.
In decimal, there will be approximately two million digits, which is about 2 megabytes of storage. This is well within the feasibility of BigInteger with a default heap size.
In practice, it even uses exponentiation by squaring to compute it quickly (though not guaranteed by the specifications). However the conversion to a String will take some time since it's a linear time operation.
import java.math.BigInteger;
public class BigPow {
public static void main(String[] args) {
BigInteger result = (new BigInteger("2")).pow(27830457).add(BigInteger.ONE);
System.out.println(result);
}
}
Here's a version which will print out the digits slowly:
import java.math.BigInteger;
public class BigPow {
public static void main(String[] args) {
BigInteger result = (new BigInteger("2")).pow(27830457).add(BigInteger.ONE);
BigInteger powten = BigInteger.TEN.pow(2357202);
while(powten.compareTo(BigInteger.TEN) > 0) {
BigInteger digit = result.divide(powten).mod(BigInteger.TEN);
System.out.print(digit);
powten = powten.divide(BigInteger.TEN);
}
}
}
The first digits are:
27337386390628313557307248857732033008168556429738078791761607160549944954510637855005417718646965163546351365984857761796847950377880836291434244529029919271706271982523405687134334692691344477538489450971091437463160940371624647030064741968436401566711255284353690448270545402444641547030399228243743315193608710148721648879085592699913299745785392609301774185427367430782834290629265859073814466687714408436025809860462926275610087354595992436000187216152954542774991509992374985538879880897902639600451627914923043483436514419544413306391278529303650112773297502090619459167888563274071587848623085880067091968911236296732119252937497152769541579516150659424997041968213122450568364121976474269097910635641227922923398092242409755554115985855831015459204780391470591543281267373716556272259386683864538263922398723602210173800151405332100275913619559563575829498369806957031526077258236305186254269056811134135133350936924294101345294335698866339561918857584229744277901180792029180156485000086528174400878657004645726892816943589969701053158760210512171516969813345080894134663207988962182426459128577282934948790911691329475034324656384238413230485050607666988301932660490870167246016897007835866691705399794247746213819662270451531049826029606671683482160663572103374
Confirmed by WolframAlpha.
I don't know why you think BigInteger isn't up to this:
import java.math.BigInteger;
public class Test {
public static void main(String[] args) throws Exception {
BigInteger big = BigInteger.valueOf(2)
.pow(7830457)
.add(BigInteger.ONE);
System.out.println(big);
}
}
It takes a little while (particularly the string conversion at the end), but it's perfectly reasonable.
As Peter noted, shifting ONE left 7830457 is much neater, mind you. I'd argue it's a bit less clear - and of course it doesn't help in the string conversion part.
EDIT: Almost all of the time is spent in the string conversion. It finished in the end on my box though. I can't see the start of it any more, but it ends with...
08570502260645006898157834607641626568029302766491883299164453304032280181734737
79366998940913082443120328458954436211937775477966920836932628607888755839700303
873
You should be able to calculate this with BigInteger.
System.out.println(BigInteger.ONE.shiftLeft(7830457).add(BigInteger.ONE));
Try something like this:
BigInteger mant = new BigInteger("2");
BigInteger result = mant.pow(7830457).add(BigInteger.ONE);

How do I use BigInterger In my code for calculating primes?

I'm using this basic and crude code below for calculating prime numbers then exporting them to a text file:
import java.util.Scanner;
import java.io.*;
public class primeGenerator{
public static void main(String[] args)throws Exception {
Scanner kb = new Scanner(System.in);
String prime;
long num = kb.nextLong();
long i;
long z=0;
while(z==0){
for (i=2; i < num ;i++ ){
long n = num%i;
if (n==0){
break;
}
}
if(i == num){
writer(num);
}
num=num+2;
}
}
public static void writer(long num) throws Exception {
FileWriter writer = new FileWriter("prime.txt",true);
String prime= ""+ num;
writer.write(prime);
writer.write(" ");
writer.flush();
writer.close();
}
}
I would like to find primes beyond the Primative long's range and apparently big integer is the way to go about it. So how do i alter my code to do so?
Do you really need this? Having numbers bigger than can be handled by long means you want to test numbers bigger than 9223372036854775807. If your for-loop can test a hundred million divisions per second, it will still take it 2923 years to determine if that number is prime - and longer for larger numbers, of course.
A common optimization is to only test divisions up to sqrt(num). If you haven't found anything then, the number is prime.
Well, use BigInteger wherever you've currently got long. Instead of using % you'll use mod, instead of incrementing you'll use i = i.add(BigInteger.ONE), instead of == 0 you'll use equals(BigInteger.ZERO) etc.
Use Scanner.nextBigInteger instead of Scanner.nextLong, too.
Given that this looks like homework of some description (possibly self-set, of course) I won't write out the whole code for you - but if you have specific problems, feel free to ask.

Categories