FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Maths/PerfectNumber.java at master · ppdouble/Java · GitHub
ppdouble
/
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
/
Maths
/
PerfectNumber.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (30 loc) · 997 Bytes
Breadcrumbs
Java
/
Maths
/
PerfectNumber.java
Copy path
File metadata and controls
32 lines (30 loc) · 997 Bytes
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
package
Maths
;
/**
* In number theory, a perfect number is a positive integer that is equal to the sum of its positive
* divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding
* itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
*
* <p>link:https://en.wikipedia.org/wiki/Perfect_number
*/
public
class
PerfectNumber
{
public
static
void
main
(
String
[]
args
) {
assert
isPerfectNumber
(
6
);
/* 1 + 2 + 3 == 6 */
assert
!
isPerfectNumber
(
8
);
/* 1 + 2 + 4 != 8 */
assert
isPerfectNumber
(
28
);
/* 1 + 2 + 4 + 7 + 14 == 28 */
}
/**
* Check if {@code number} is perfect number or not
*
* @param number the number
* @return {@code true} if {@code number} is perfect number, otherwise false
*/
public
static
boolean
isPerfectNumber
(
int
number
) {
int
sum
=
0
;
/* sum of its positive divisors */
for
(
int
i
=
1
;
i
<
number
; ++
i
) {
if
(
number
%
i
==
0
) {
sum
+=
i
;
}
}
return
sum
==
number
;
}
}
Back
|
FazBrowse Home
|
New Git URL