-
-
Save fahadsadah/290515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void main() { | |
int maxterms = 1; | |
int maxstarter = 1; | |
int x; | |
for (original = 1; original <=1000000; original++) { | |
int x = original; | |
int terms = 1; | |
while (x != 1) { | |
if (x % 2 == 0) { | |
//x is even | |
x = x/2; | |
} | |
else { | |
//x is odd | |
x = (3 * x) + 1; | |
} | |
terms = terms + 1; | |
} | |
if (terms > maxterms) { | |
maxstarter = original; | |
maxterms = terms; | |
} | |
printf("%d gives %d terms\n", original, terms); | |
} | |
printf("%d gives %d terms\n", maxstarter, maxterms); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
maxterms = 1 | |
maxstarter = 1 | |
(1..1000000).each do | |
|x| | |
original = x | |
terms = 1 | |
until x == 1 do | |
if x % 2 == 0 then | |
#x is even | |
x = x/2 | |
else | |
#x is odd | |
x = (3 * x) + 1 | |
end | |
terms = terms + 1 | |
end | |
if terms > maxterms then | |
maxstarter = original | |
maxterms = terms | |
end | |
print "#{original} gives #{terms} terms\n" | |
end | |
print "#{maxstarter} gives #{maxterms} terms\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment