Skip to content

Instantly share code, notes, and snippets.

@fahadsadah
Created January 30, 2010 11:20
Show Gist options
  • Save fahadsadah/290515 to your computer and use it in GitHub Desktop.
Save fahadsadah/290515 to your computer and use it in GitHub Desktop.
#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);
}
#!/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