FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/ProjectEuler/Problem01.java at master · oribach/Java · GitHub
oribach
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
1
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
/
Problem01.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (43 loc) · 1.06 KB
Breadcrumbs
Java
/
ProjectEuler
/
Problem01.java
Copy path
File metadata and controls
51 lines (43 loc) · 1.06 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
package
ProjectEuler
;
/**
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
* The sum of these multiples is 23.
*
* <p>Find the sum of all the multiples of 3 or 5 below 1000.
*
* <p>Link: https://projecteuler.net/problem=1
*/
public
class
Problem01
{
public
static
void
main
(
String
[]
args
) {
int
[][]
testNumber
= {
{
3
,
0
},
{
4
,
3
},
{
10
,
23
},
{
1000
,
233168
},
{-
1
,
0
}
};
for
(
int
[]
ints
:
testNumber
) {
assert
solution1
(
ints
[
0
]) ==
ints
[
1
];
assert
solution2
(
ints
[
0
]) ==
ints
[
1
];
}
}
private
static
int
solution1
(
int
n
) {
int
sum
=
0
;
for
(
int
i
=
3
;
i
<
n
; ++
i
) {
if
(
i
%
3
==
0
||
i
%
5
==
0
) {
sum
+=
i
;
}
}
return
sum
;
}
private
static
int
solution2
(
int
n
) {
int
sum
=
0
;
int
terms
= (
n
-
1
) /
3
;
sum
+=
terms
* (
6
+ (
terms
-
1
) *
3
) /
2
;
terms
= (
n
-
1
) /
5
;
sum
+=
terms
* (
10
+ (
terms
-
1
) *
5
) /
2
;
terms
= (
n
-
1
) /
15
;
sum
-=
terms
* (
30
+ (
terms
-
1
) *
15
) /
2
;
return
sum
;
}
}
Back
|
FazBrowse Home
|
New Git URL