1
2function primeFactors(n){
3 var factors = [],
4 divisor = 2;
5
6 while(n>2){
7 if(n % divisor == 0){
8 factors.push(divisor);
9 n= n/ divisor;
10 }
11 else{
12 divisor++;
13 }
14 }
15 return factors;
16}
17
18> primeFactors(69);
19 = [3, 23]
20
1def primeFactors(n):
2 global factors
3 if(n==1):
4 return
5 elif((n%factors)== 0):
6 print(factors)
7 primeFactors(n//factors)
8 else:
9 factors+=1
10 primeFactors(n)
11factors = 2
1public List<Integer> factorsOf(int n) {
2 ArrayList<Integer> factors = new ArrayList<>();
3
4 for (int d = 2; n > 1; d++)
5 for (; n % d == 0; n /= d)
6 factors.add(d);
7
8 return factors;
9}
10
1>>> for n in range(2, 10):
2... for x in range(2, n):
3... if n % x == 0:
4... print(n, 'equals', x, '*', n//x)
5... break
6... else:
7... # loop fell through without finding a factor
8... print(n, 'is a prime number')
9...
102 is a prime number
113 is a prime number
124 equals 2 * 2
135 is a prime number
146 equals 2 * 3
157 is a prime number
168 equals 2 * 4
179 equals 3 * 3
18