-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerBreak343.java
More file actions
25 lines (24 loc) · 873 Bytes
/
Copy pathIntegerBreak343.java
File metadata and controls
25 lines (24 loc) · 873 Bytes
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
/*
Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.
Return the maximum product you can get.
*/
public class IntegerBreak343 {
public static void main(String[] args) {
System.out.println(integerBreak(10));
System.out.println(integerBreak(2));
System.out.println(integerBreak(8));
}
public static int integerBreak(int n) {
double max = 0;
for (int i = 2; i <= n; i++) {
int math = n / i;
if (n % i == 0) max = Math.max(max, Math.pow(math, i));
else {
int mod = n % i;
if (mod == i - 1) max = Math.max(max, Math.pow(math + 1, i - 1) * math);
else max = Math.max(max, Math.pow(math, i - 1) * (math + mod));
}
}
return (int) max;
}
}