0

I was given this question.

n = 77

n = p*q

p and q is a prime number

Make the finder of p and q with brute force.

My code so far:

public class If {

    public static void main(String[] args) {

        int p = 3, q = 3;
        int n = 77;
        int temp = p*q;
        boolean flagp, flagq = false;
        while (temp != n && p <= 77)
        {
            for(int i = 2; i <= p/2; ++i)
            {
                // condition for nonprime number
                if(p % i == 0)
                {
                    flagp = true;
                    break;
                }
                p = p+2;
                q = 3;
                for(int j = 2; j <= q/2; ++j)
                {
                    // condition for nonprime number
                    if(q % j == 0)
                    {
                        flagq = true;
                        break;
                    }
                    q = q+2;
                    temp = p*q;
                }
            }
        }
        System.out.println(temp);
    }
}

I was able to find the prime number checking. But I can't seem to find how loop it and find the matching p and q.

Tome
  • 3,234
  • 3
  • 33
  • 36
  • You could first find all prime-numbers and save them in a list. Then you could use two nested for-loops to check which combination works. – Christian Jul 18 '17 at 10:56
  • Don't declare i and j to be local inside your for loops. You need those values when you break. Half your other variables are redundant. This includes p, q, temp, flagp, flagq. – Necreaux Jul 18 '17 at 10:58
  • I could think about list all prime numbers smaller than `n`. Loop through the list and assume it's `p`. Calculate the division `n/p` => `q`. Check if `q` is prime or not. – Manh Le Jul 18 '17 at 11:02
  • I'm bad with list and array. Suppose I can make a list of prime number. How do I input to 2 arrays and then check it? – Christian Hardjono Jul 18 '17 at 11:06
  • so I need to initiate p, q, and so on outside the loops? – Christian Hardjono Jul 18 '17 at 11:07
  • that could work, but again, I'm bad with array. I'm gonna try google about array first. – Christian Hardjono Jul 18 '17 at 11:09

4 Answers4

2

You don't need a loop for p and one for q. Whenever you find a q such that n%q == 0, you can calculate p = n/q. Then, make a function to check if p and q are both prime numbers, and if they are, stop the loop execution and print them.

Brute force edit: my bad, brute force is not my thing, our teachers close us into the uni basement and hit us with chains if we use it to solve certain problems. So, the way to use brute force here is simply multiplying all possible p and q from 2 to n/2 and check if p*q == n. No more optimizations or restrictions to make it a beautiful and slow brute force algorithm.

PD: Now I've noticed, maybe this isn't actually brute force and algorithms classes have disturbed my mind. Thank god I haven't gone with Euler's theorem.

Shinra tensei
  • 1,283
  • 9
  • 21
2
import java.math.*;
import java.io.*;
class If {
  public static void main(String[] args) {
    int n=77, p=2, q=0;
    while (n%p>0) { p++; }
    q=77/p;
    System.out.println(new BigInteger(""+q).isProbablePrime(1)?p+" "+q:"No solution exists");
  }
}

EDIT: a little more useful solution

String out="";
String primeFactor(int n) {
  int p=2;
  while (n%p>0 && p<=n){p++;}
  out+=p;
  if (p<n){
    out+=" ";
    primeFactor(n/p);
  }
  return out;
}
System.out.println(primeFactor(77));
PrincePolka
  • 196
  • 1
  • 9
1

I have a solution for you (using BigInteger) :

import java.math.BigInteger;

public class If {

    //The first prime number
    public static final BigInteger INIT_NUMBER = new BigInteger("2");

    public static void main(String[] args) {

        //Initialise n and p
        BigInteger n = new BigInteger("77");
        BigInteger p = INIT_NUMBER;

        //For each prime p
        while(p.compareTo(n.divide(INIT_NUMBER)) <= 0){

            //If we find p
            if(n.mod(p).equals(BigInteger.ZERO)){
                //Calculate q
                BigInteger q = n.divide(p);
                //Displays the result
                System.out.println("(" + p + ", " + q + ")");
                //The end of the algorithm
                return;
            }
            //p = the next prime number
            p = p.nextProbablePrime();
        }
        System.out.println("No solution exists");
    }
}

Note : The BigInteger class contains many functions to manipulate the prime numbers. This saves a lot of time and avoids the calculation errors associated with large numbers.

Valentin Genevrais
  • 421
  • 1
  • 6
  • 21
0

The General Number Field Sieve (GNFS) algorithm is the most efficient algorithm to find prime factors (up to now), but it is more difficult to program than the ones cited above. If you deal with really big numbers, you should use GNFS.

Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24