简体   繁体   English

从p和q为质数时找出n = p * q的'p'和'q'

[英]Finding a 'p' and 'q' of n=p*q from when p and q are prime numbers

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. 用蛮力使p和q的发现者。

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 . 但是我似乎无法找到如何循环并找到匹配的pq

You don't need a loop for p and one for q. 您不需要为p循环,而为q循环。 Whenever you find aq such that n%q == 0 , you can calculate p = n/q . 只要找到a使得n%q == 0 ,就可以计算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. 然后,创建一个函数来检查p和q是否均为素数,如果它们均为素数,则停止循环执行并打印它们。

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 . 因此,此处使用蛮力的方法就是将所有可能的p和q从2乘以n / 2并检查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. PD:现在我注意到了,也许这实际上并不是蛮力的,算法类困扰了我。 Thank god I haven't gone with Euler's theorem. 谢天谢地,我没有遵循欧拉定理。

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));

I have a solution for you (using BigInteger) : 我有一个适合您的解决方案(使用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. 注意 :BigInteger类包含许多操作素数的函数。 This saves a lot of time and avoids the calculation errors associated with large numbers. 这样可以节省大量时间,并避免了与大数相关的计算错误。

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. 通用数字字段筛选(GNFS)算法是迄今为止找到素因数的最有效算法,但是比上面引用的算法难编程。 If you deal with really big numbers, you should use GNFS. 如果要处理非常大的数字,则应使用GNFS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据p + q和pq计算p ^ n + q ^ n - Computing p^n + q^n from p+q and pq RSA加密-查找P和Q - RSA Encryption - Finding P and Q 在java中生成随机数对,使得p!= q - generating pairs of random numbers in java such that p !=q 布尔值(p ^ q)和(p!= q)之间是否存在有用的差异? - Is there a useful difference between (p ^ q) and (p != q) for booleans? 在Android P和Q Beta中以编程方式切换异常时的意外行为 - Unexpected behaviour when programmatically toggling exceptions in Android P and Q Beta 如何从Java的KeyPairGenerator for RSA获取P和Q? - How to get P and Q from KeyPairGenerator for RSA in Java? 有没有一种优雅的方法将Map <P,Optional <Q >>转换为稀疏Map <P,Q>? - Is there an elegant way to convert a Map<P, Optional<Q>> to a sparse Map<P, Q>? 找到所有整数 p 和 q 的优化方法,使得 2^p*3^q 具有给定的十进制长度 - Optimised way to find all integers p and q such that 2^p*3^q has a given decimal length 如果存在另一个点 (p,q) 使得 x &lt; p 且 y &lt; q,则返回数组中的一个点 (x,y) - Returning a point (x,y) in a array if there is another point (p,q) such that x < p and y < q 如何在Java中使用split函数拆分像p ^ q这样的字符串? - How to split a string like p^q using split function in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM