Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ProjectEuler Solution for 14 (#2138) #2194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter
Filter file types
Beta Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,58 @@
package ProjectEuler;
/**
* The following iterative sequence is defined for the set of positive
* integers:
*
* <p>n → n/2 (n is even)
* <p>n → 3n + 1 (n is odd)
*
* <p>Using the rule above and starting with 13, we generate the following
* sequence:
*
* <p>13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
*
* <p>It can be seen that this sequence (starting at 13 and finishing at 1)
* contains 10 terms. Although it has not been proved yet (Collatz Problem),
* it is thought that all starting numbers finish at 1.
*
* <p>Which starting number, under one million, produces the longest chain?
*
* <p>NOTE: Once the chain starts the terms are allowed to go above one
* million.
*/
public class Problem14 {
public static void main(String[] args) {
assert solution1(1000000) == 837799;
}

static int getSequenceLength(long num) {
int length = 1;

while (num > 1) {
if (num % 2 == 0) {
num = num / 2;
} else {
num = 3 * num + 1;
}
length++;
}

return length;
}

public static int solution1(int maxNumber) {
int maxLength = 0;
int num = 0;
int len;

for (int i = 1; i <= maxNumber; i++) {
len = getSequenceLength(i);
if (len > maxLength) {
maxLength = len;
num = i;
}
}

return num;
}
}
ProTip! Use n and p to navigate between commits in a pull request.