FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/ProjectEuler/Problem03.java at master · debugmm/Java · GitHub
debugmm
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
ProjectEuler
/
Problem03.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (58 loc) · 1.3 KB
Breadcrumbs
Java
/
ProjectEuler
/
Problem03.java
Copy path
File metadata and controls
62 lines (58 loc) · 1.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package
ProjectEuler
;
/**
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* <p>What is the largest prime factor of the number 600851475143 ?
*
* <p>Link: https://projecteuler.net/problem=3
*/
public
class
Problem03
{
/**
* Checks if a number is prime or not
*
* @param n the number
* @return {@code true} if {@code n} is prime
*/
public
static
boolean
isPrime
(
int
n
) {
if
(
n
==
2
) {
return
true
;
}
if
(
n
<
2
||
n
%
2
==
0
) {
return
false
;
}
for
(
int
i
=
3
,
limit
= (
int
)
Math
.
sqrt
(
n
);
i
<=
limit
;
i
+=
2
) {
if
(
n
%
i
==
0
) {
return
false
;
}
}
return
true
;
}
/**
* Calculate all the prime factors of a number and return the largest
*
* @param n integer number
* @return the maximum prime factor of the given number
*/
static
long
maxPrimeFactor
(
long
n
) {
for
(
int
i
=
2
;
i
<
n
/
2
;
i
++) {
if
(
isPrime
(
i
))
while
(
n
%
i
==
0
) {
n
/=
i
;
}
}
return
n
;
}
public
static
void
main
(
String
[]
args
) {
int
[][]
testNumbers
= {
{
87
,
29
},
{
10
,
5
},
{
21
,
7
},
{
657
,
73
},
{
777
,
37
}
};
for
(
int
[]
num
:
testNumbers
) {
assert
Problem03
.
maxPrimeFactor
(
num
[
0
]) ==
num
[
1
];
}
assert
Problem03
.
maxPrimeFactor
(
600851475143L
) ==
6857
;
}
}
Back
|
FazBrowse Home
|
New Git URL