* If x is even, halve it
* If x is odd, triple it and add one
* Until x is 1
Anyway, I am trying (make your own reasons up) to create this procedure in as many programming languages as I can, and so am asking help in teh programmers' guild. '99 bottles of beer on the wall' has clocked up about 612 languages; it might be interesting to see how many this one can.
Javascript:
var x = 10; // The number
var n = 1; // The count
do {
n++; // Increase count
document.write(x+" "); // Output x
x = next(x); // Change x
} while (x > 1); // Stop if x is 1
document.write("1<br>(in "+n+" steps)");
function next(k) { if (k%2 == 0) return k/2; return 3*k+1; }
int next(int n) { if (n%2==0) return n >> 1; else return n*3+1; }
int main() {
int x = 10;
int n = 0;
while (x > 1) {
x = next(x);
printf("x: %i\n",x);
}
return 0;
}
Edited by the Author.
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
int next(int n) { if (n%2==0) return n >> 1; else return n*3+1; }
int main() {
int x = 10;
int n = 0;
while (x > 1) {
x = next(x);
cout << "x: " << x << "\n";
}
return 0;
}
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
#!/usr/bin/perl
## the above isn't necessary for windows
print "content-type: text/html\n\n"; # following output is html
print "<html><head><title>CGI version</title></head><body>";
Show me the power child,
I'd like to say,
That I'm down on my knees today,
Gives me the butterflies,
Gives me away,
'Til I'm up on my feet again,
I'm feeling outshined.
Private Sub NoName()
x = 10
Do until x = 1
if x / 2 = int(x / 2) and x <> 1 then
x = x /2
list1.additem x ' You can also do Print or Debug.Print
elseif x / 2 <> int(x / 2) and x <> 1 then
x = 3 * x + 1
list1.additem x ' You can also do Print or Debug.Print
end if
loop
end sub
True Basic: Same thing except replace list1.additem x to PRINT x, get rid of the word "Private", and get rid of the comments
10 LET C = 0
20 INPUT "Enter a number", X
30 WHILE X > 1
40 LET C = C + 1
50 IF INT(X/2) = X/2 THEN LET X = X/2 ELSE LET X = (X*3)+1
60 PRINT C, ": ", X
70 END WHILE
80 PRINT "X became 1 in ", C, " steps"
90 END
I think line 50 would work, but it's been ages since I used a Commodore, of course. The way I'm testing for it being an even number is testing to see if X/2 is the same as X/2 truncated... hopefully I got the command right. Oh, and END WHILE might be WEND, I really can't remember.