Skip to content

Commit 680d1cc

Browse files
authored
Merge pull request TheAlgorithms#1 from vtolas/vtolas-patch-1
Create PrimeFactorization.java
2 parents 21bd91a + d41fc15 commit 680d1cc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Maths/PrimeFactorization.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Maths;
2+
3+
import java.lang.Math;
4+
import java.util.Scanner;
5+
6+
public class algorithm {
7+
public static void main(String[] args){
8+
System.out.println("## all prime factors ##");
9+
Scanner scanner = new Scanner(System.in);
10+
System.out.print("Enter a number: ");
11+
int n = scanner.nextInt();
12+
System.out.print(("printing factors of " + n + " : "));
13+
pfactors(n);
14+
}
15+
public static void pfactors(int n){
16+
17+
while (n%2==0)
18+
{
19+
System.out.print(2 + " ");
20+
n /= 2;
21+
}
22+
23+
for (int i=3; i<= Math.sqrt(n); i+=2)
24+
{
25+
while (n%i == 0)
26+
{
27+
System.out.print(i + " ");
28+
n /= i;
29+
}
30+
}
31+
32+
if(n > 2)
33+
System.out.print(n);
34+
}
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy