Home Misc Prime factors of a number. [Prime Factorization]

# Prime factors of a number. [Prime Factorization]

0 comment

Question: Given an integer N, find all the prime factors of the number.

Input:
N = 45

Output: 3 5

Finding prime factors of any numbers is a very important concept in number theory. One sample problem is available here in which we have to find the largest prime factor.

The mathematical concept behind this problem is simple. We start with ‘2’ and keep dividing the original number to get rid of composite factors. Continue this process until the number is no longer divisible by 2. This means that at this point, we do not have any multiples of 2 in the factors.

Example: 48 = 2 * 2 * 2 * 2 * 3
If we divide it by 2 four times we get rid of all the composite factors.

• Now we proceed with ‘3’ and so on to get rid of all the factors.
• Keep on adding these numbers to a set as they are left out.
• The reason these numbers are left out is because they are prime numbers.
• Hence, at the end of our loop we have all our prime factors in the set.

The code shown below implements the above algorithm.

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
.hljs > mark.shcb-loc { background-color: #ddf6ff; }import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class PrimeFactorisation {

/**
* Created by studyalgorithms.com
*/

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
long N = scanner.nextLong();
Set<Long> primeFactors = new HashSet<>();

while (N % 2 == 0) {
N /= 2;
}

for (long j = 3; j <= Math.sqrt(N); j += 2) {
while (N % j == 0) {
N /= j;
}
}

if (N > 2)

for (Long primeFactor : primeFactors) {
System.out.print(primeFactor + " ");
}

}
}
Code language: Java (java)

A working implementation of the code can be found here.

#### You may also like

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More