Decomposition of number into prime numbers

杀马特。学长 韩版系。学妹 提交于 2019-12-07 15:55:46

问题


I'm trying to create a pascal program for decomposition of prime numbers, i.e.

16 = 2*2*2*2
210 = 2*3*5*7

I should input a number and I should return the prime numbers decomposition. I don't understand the solution in mathematical sense, could someone explain me this algorithm or pseudo code, as long as I understand what I'm creating programming isn't really an issue.

Thanks


回答1:


A naive way of doing this is to:

k = 2
N = 210
while N > 1:
    if N % k == 0:   // if k evenly divides into N
        print k      // this is a factor
        N = N / k    // divide N by k so that we have the rest of the number left.
    else:
        k = k + 1

The main premise, is that FACTOR(N) is equal to k * FACTOR(N / k), if k divides N. So keep doing this over and over until you can no longer factor N. This way, you get k1 * k2 * k3 * FACTOR(N / k1 / k2 / k3) etc.

If you start with small numbers and work up, you'll only pull out the prime factors.

So, for 210, you get:

k = 2
N = 210

k divides N, so print 2, N becomes 105
k no longer divides N, so k becomes 3
k divides N, so print 3, N becomes 35
k no longer divides N, so k becomes 4
k does not divide N, so k becomes 5
k divides N, so print 5, N becomes 7
k no longer divide N, so k becomes 6
k does not divide N, so k becomes 7
k divides N, so print 7, N becomes 1
N is now equal to 1, so stop.

You get 2 3 5 7 

A basic improvement would be that you only have to iterate through the primes. So, you could have skipped over 4 and 6 in the above example.




回答2:


A prime number is an integer with exactly two divisors. For example, 13 is prime, since it is divisible only by 1 and by 13 (two divisors). 14 is not prime, since it is divisible by 1, 2, 7 and 14 (four divisors). 4 is not prime, since it is divisible by 1, 2 and 4 (three divisors). By convention, 1 is not a prime number, but that's not really important here.

Every integer larger than 1 has a unique factorization (decomposition) into prime numbers. In other words, there is only one (multi)set of prime numbers such that their product is equal to the given number. For example:

14 = 2 * 7
16 = 2 * 2 * 2 * 2
4 = 2 * 2
13 = 13

Your task is to write an algorithm that takes on input an integer larger than 1 and outputs a list of prime numbers such that their product is equal to the input number.

Arguably the easiest algorithm for factorization is trial division.




回答3:


There are two cases to consider with the number you get as input:

1) The number is a prime number (in which case there is no factorization possible. You should just return the number as output)

2) The number is not a prime number (It can be factored into product of primes)

I will outline the steps below. Note that I am using another famous algorithm. I do not know if there is a more efficient way of doing this.

1) Use Sieve Of Eratosthenes algorithm ( http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes ) to find all the primes less than your input. In this process you can also determine if your input is a prime number.

2) Now if your number is not a prime, see the first prime that divides it and continue tracking each of the number you get as quotient.

Here is a nice illustration: http://www.youtube.com/watch?v=9m2cdWorIq8

Example:

Suppose you receive input as 12.

(Operation , Input , Output)

       -      12     -
     12/2     6      2
     6/2      3      2*2
     3/2      3      2*2
     3/3      1      2*2*3

The algorithm stops if you hit a prime or 1 in the Input field.

As you can see the key here is to know the primes (2, 3, 5 ...) so that you can divide your input with them. Also you need to determine if your input is prime. Both can be accomplished with Sieve of Eratosthenes.




回答4:


based on Donald Miner answer, I did a bash function:

function decompose(){
    r=$(
            k=2;
            N=$1;
            while [ $N -gt 1 ] && [ $(( k ** 2 )) -le $N ];
            do [ "$(( N % k ))" == 0 ] && 
            {
                    echo $k; N=$(( N/k ));
            } || let k++; 
            done
            [ $(( k ** 2 )) -gt $N ] && echo $N
    );
    echo "$r" | uniq | 
    while read n;
    do echo "$n^$(
            echo "$r" | grep '^'"$n"'$' | wc -l
    )";
    done |
    tr '\n' '*' | 
    sed 's/\(\^1\)\?\*$//g; s/\^1\([^0-9]\)/\1/g'; 
    echo;
}

Some samples:

$ decompose 768
2^8*3
$ decompose 110
2*5*11
$ decompose 686567
7*98081
$ echo "7*98081" | bc
686567

As said, it can be much faster if get the prime number instead of increment k but seems good to me.



来源:https://stackoverflow.com/questions/7755771/decomposition-of-number-into-prime-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!