簡體   English   中英

如何使用循環找到數字的質因數分解?

[英]How to find prime factorization of number using loop?

我應該找到這個數字的質因數分解:600851475。

根據我的老師和這個網站http://www.mathsisfun.com/prime-factorization.html 的說法,素數分解是素數乘以得到那個數字。 因此,例如 12,即使它的因數是 2、3、4、6,素因數也不會只是 2 和 3,而是 2、2、3。

我已經有了找到素因數的算法,但我找不到循環的方法,以便它不斷找到其余的,直到沒有更多的素因數。

這是我得到的:

public class primeFactors {
public static void main(String[] args) {
int d= 600851475;
int i = 2;
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
}
}

它打印出這個:3。

如果我多次復制粘貼它會打印不同的東西:

public class primeFactors {
public static void main(String[] args) {
int d= 600851475;
int i = 2;
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);

if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);

if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);

if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
}
}

那個打印:3, 3, 4, 5, 5。我怎樣才能用循環來做到這一點? 我嘗試使用 do while 循環( do { if section} while (d>i) {print i} ),但它不起作用。 我也試過 for 循環 (i=2;i<=d;i++) & 它不起作用。 它也給了我合數。 請幫忙!!

由於這是一項作業,因此我最多只能給你一個大致的方向:寫這個的最簡單方法是嘗試候選除數並減少。 例如:

130 - 嘗試 2,它會分開,所以減少 65 - 也許那里還有另外 2? 再試2。 它不會分裂,所以繼續 65 - 嘗試 3 - 不。 4? 不。 5? 是的,它會分裂,所以減少。 13 - 里面還有 5 個嗎? 不。 嘗試 6, 7, 8, 9, 10, 11, 12. 好的,你完成了。

因此,您需要在循環中嘗試候選除數,並且需要一個內部循環來確保排除任何重復的因數(例如,525 將具有質因數 3、5 和 7,但您仍然想擺脫那第二個5)。 這應該讓你走上正軌。

顯然,會有更有效的方法來編寫它,但是如果您遇到困難,請從最簡單的可行方法開始,然后讓它發揮作用。

你只需要這樣的東西(也許這不是最有效的方法,但它非常簡單易懂):

int d= 600851475;

for (int i = 2 ; i < (d / 2) ; i++){
    if(isPrime(i)){
        if(d % i ==0){
            System.out.println(i + "Is a prime factor of " + d);
        }
    }
}

但首先你需要有這個方法來檢查它是否是素數

public static boolean isPrime(int n){
    for(int i = 2 ; i < n ; i++){
        if(n % i == 0){
            return false; 
        }
    }
    return true;
}

嘗試使用 for 循環。

for (int i = 2; i <= d; i++)
{
  //implement if statement here
}

這應該為您指明正確的方向。

你有正確的想法。 您需要對每個因子進行除法,直到它不再除法為止。

// this will print all prime divisors for n
for (int i = 2; n != 1; i++)
{
    while (n % i == 0)
    {
        System.out.println (i);
        n /= i;
    }
}

如何找到一個數的質因數分解?

這是我嘗試過的:

def p_factorization(n):
    #Finding the factors of n.
    num=[i for i in range(1,n+1) if n%i==0]
    #Finding the prime factors of n.
    #Now prime_num checks to see if any of the factors of 36 have more than two "subfactors".
    prime_num=[i for i in range(2,max(num)) if len([j for j in range(1,i+1) if i%j==0])<=2 and max(num)%i==0]
    return prime_num
#Explanation:
#The num list is self-explanatory. It finds all of the factors of the number n.
#The prime_num list comprehension is where it gets complicated:
#The i represents all of the numbers in the range of the original factors that we found, 
#then we know that a number is prime if it has two or fewer factors, hence the if condition 
#applying to the len of the factors of i, which is being iterated through. 
#For example, 4 is not a prime number because the length of the list of factors is 
#3 [1,2,4]. 

當然,我們仍然需要 max(num) 可以被 i 整除。

不客氣!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM