Skip to content

Commit 525dc2e

Browse files
committed
Prime Factorization added
1 parent a7b7e83 commit 525dc2e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class PrimeFactorization
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner sc=new Scanner(System.in); //Scanner class
9+
10+
int n=sc.nextInt(); //Raeding user input
11+
12+
primeFactorization(n);
13+
}
14+
public static void primeFactorization(int n)
15+
{
16+
/*If n is a factor of 2,we print 2 and
17+
then check whether (n/2)%2 is zero or not*/
18+
while (n%2==0)
19+
{
20+
System.out.print(2+" ");
21+
n=n/2;
22+
}
23+
24+
/*Above loop exits only when n becomes odd,so now n is odd,
25+
so we can skip all the even elements in the for loop i.e i=i+2*/
26+
for (int i=3;i<=Math.sqrt(n);i+=2)
27+
{
28+
/*if i is a factor of n,then print i and then
29+
divide n by i till i is not a factor of i*/
30+
while (n%i==0)
31+
{
32+
System.out.print(i+" ");
33+
n/=i;
34+
}
35+
}
36+
/*condition for the prime numbers
37+
greater than 2,we directly print n*/
38+
if (n>2)
39+
System.out.print(n);
40+
}
41+
42+
43+
}

Java/PrimeFactorization/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample Input:
2+
36
3+
4+
# Sample Output:
5+
2 2 3 3
6+
7+
# Sample Input:
8+
11
9+
10+
# Sample Output:
11+
11
12+
13+
# Explaination:
14+
<h3>TC 1: 2 * 2 * 3 * 3 =36</h3>
15+
<h3>TC 2: 11 , since 11 itself is a prime number and a prime factor of itself.</h3>
16+
17+
<p>The while loop and for loop take care of composite numbers and last condition takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.</p>
18+
19+
20+
# Time Complexity:
21+
<h3>O(sqrt(n))</h3>
22+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Relevance in Cryptography:
2+
3+
Cryptography is all about number theory, and all integer numbers (except 0 and 1) are made up of primes, so you deal with primes a lot in number theory.
4+
5+
More specifically, some important cryptographic algorithms such as RSA critically depend on the fact that prime factorization of large numbers takes a long time. Basically you have a "public key" consisting of a product of two large primes used to encrypt a message, and a "secret key" consisting of those two primes used to decrypt the message. You can make the public key public, and everyone can use it to encrypt messages to you, but only you know the prime factors and can decrypt the messages. Everyone else would have to factor the number, which takes too long to be practical, given the current state of the art of number theory.

0 commit comments

Comments
 (0)