* 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.
if "%1"=="" goto noarg
if "%1"=="halve" goto halve
if "%1"=="bytreeplusone" goto by3plus1
if "%1"=="evenoddchoice" goto evenoddt
if "%1"=="evenoddtest" goto evenodd2
if "%1"=="icumul" goto icumul
if "%1"=="valtostr" goto valtostr
if "%1"=="addone" goto addone
if "%1"=="moreenvmem" goto moremem
set cumul=
if "%1"=="nochoice" goto batch2
:: protection against misconfigured system
choice /? > tempch.ch
if not exist tempch.ch goto nochoice
del tempch.ch
@%comspec% /e:32768 /c %thispath% moreenvmem %1
goto fin
:moremem
shift
:: first of all , let's parse the argument
choice /c:²%1²² /t:²,1 @%thispath% nochoice > TempFil.bat
TempFil.bat
goto dltmpfin
:batch2
if exist TempFil.bat del TempFil.bat
shift
set cumul=
:2shift
shift
set result=
if "%1"=="0" set result=I
if "%1"=="1" set result=II
if "%1"=="2" set result=III
if "%1"=="3" set result=IIII
if "%1"=="4" set result=IIIII
if "%1"=="5" set result=IIIIII
if "%1"=="6" set result=IIIIIII
if "%1"=="7" set result=IIIIIIII
if "%1"=="8" set result=IIIIIIIII
if "%1"=="9" set result=IIIIIIIIII
if not "%cumul%"=="" set cumul=%cumul% %result%
if "%cumul%"=="" set cumul=%result%
if "%2"=="" goto endshift
if "%2"=="²" goto endshift
if "%2"=="²]?²" goto endshift
if not "%2"=="" goto 2shift
set cumul=%cumul%
set result=
:endshift
set result=
set loop=I
call %thispath% evenoddchoice
goto fin
if "%numtype%"=="even" call %thispath% halve %cumul%
if "%numtype%"=="odd" call %thispath% icumul %cumul%
goto evenoddt
:tendloop
set thispath=
call %thispath% valtostr inverted %loop%
echo done in %num% loops
set numtype=
set icumul=
set thispath=
set pair=
set result=
set odd=
set cumul=
set nbleft=
set nbreste=
set reverse=
set one=
set loop=
goto fin
:evenodd2
shift
if not "%2"=="" goto evenodd2
if "%1"=="I" set numtype=even
if "%1"=="II" set numtype=odd
if "%1"=="III" set numtype=even
if "%1"=="IIII" set numtype=odd
if "%1"=="IIIII" set numtype=even
if "%1"=="IIIIII" set numtype=odd
if "%1"=="IIIIIII" set numtype=even
if "%1"=="IIIIIIII" set numtype=odd
if "%1"=="IIIIIIIII" set numtype=even
if "%1"=="IIIIIIIIII" set numtype=odd
goto fin
:icumul
set icumul=
:icumul2
shift
set icumul=%1 %icumul%
if not "%2"=="" goto icumul2
call %thispath% bytreeplusone %icumul%
set icumul=
goto fin
:halve
set odd=
set pair=
set cumul=
:halvloop
shift
set result=
if "%1"=="I" set result=I
if "%1"=="III" set result=II
if "%1"=="IIIII" set result=III
if "%1"=="IIIIIII" set result=IIII
if "%1"=="IIIIIIIII" set result=IIIII
if "%1"=="II" set odd=yes
if "%1"=="II" set result=I
if "%1"=="IIII" set odd=yes
if "%1"=="IIII" set result=II
if "%1"=="IIIIII" set odd=yes
if "%1"=="IIIIII" set result=III
if "%1"=="IIIIIIII" set odd=yes
if "%1"=="IIIIIIII" set result=IIII
if "%1"=="IIIIIIIIII" set odd=yes
if "%1"=="IIIIIIIIII" set result=IIIII
if "%pair%"=="yes" set result=%result%IIIII
set pair=%odd%
set odd=
:: if "%2abcdef%result%"=="abcdefI" goto halvefin
if not "%cumul%"=="" set cumul=%cumul% %result%
if "%cumul%"=="" set cumul=%result%
if "%cumul%"=="I" set cumul=
if not "%2"=="" goto halvloop
:halvefin
set result=
set odd=
set pair=
goto fin
:by3plus1
set result=
set nbleft=I
set cumul=
:b3p1loop
shift
set nbreste=%nbleft%
set nbleft=
set result=
:: multiply by 3
if "%1"=="I" set result=I
if "%1"=="II" set result=IIII
if "%1"=="III" set result=IIIIIII
if "%1"=="IIII" set result=IIIIIIIIII
if "%1"=="IIIII" set result=III
if "%1"=="IIIII" set nbleft=I
if "%1"=="IIIIII" set result=IIIIII
if "%1"=="IIIIII" set nbleft=I
if "%1"=="IIIIIII" set result=IIIIIIIII
if "%1"=="IIIIIII" set nbleft=I
if "%1"=="IIIIIIII" set result=II
if "%1"=="IIIIIIII" set nbleft=II
if "%1"=="IIIIIIIII" set result=IIIII
if "%1"=="IIIIIIIII" set nbleft=II
if "%1"=="IIIIIIIIII" set result=IIIIIIII
if "%1"=="IIIIIIIIII" set nbleft=II
set result=%result%%nbreste%
:: rest could give an other rest
if "%result%"=="IIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIII" set result=I
if "%result%"=="IIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIII" set result=II
if "%result%"=="IIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIII" set result=III
if "%result%"=="IIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIII" set result=IIII
if "%result%"=="IIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIII" set result=IIIII
if "%result%"=="IIIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIIII" set result=IIIIII
if "%result%"=="IIIIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIIIII" set result=IIIIIII
if "%result%"=="IIIIIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIIIIII" set result=IIIIIIII
if "%result%"=="IIIIIIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIIIIIII" set result=IIIIIIIII
if "%result%"=="IIIIIIIIIIIIIIIIIIII" set nbleft=I%nbleft%
if "%result%"=="IIIIIIIIIIIIIIIIIIII" set result=IIIIIIIIII
if "%result%"=="IIIIIIIIIIIIIIIIIIIII" set nbleft=II%nbleft%
if "%result%"=="IIIIIIIIIIIIIIIIIIIII" set result=I
if not "%cumul%"=="" set cumul=%result% %cumul%
if "%cumul%"=="" set cumul=%result%
if not "%2"=="" goto b3p1loop
:: the result could have a larger number than expected
if "%nbleft%"=="" goto fin
set cumul=I%nbleft% %cumul%
set result=
set nbleft=
set nbreste=
goto fin
:valtostr
shift
if "%1"=="reverse" set reverse=yes
if "%1"=="reverse" shift
set num=
set result=
:valloop1
if "%1"=="I" set result=0
if "%1"=="II" set result=1
if "%1"=="III" set result=2
if "%1"=="IIII" set result=3
if "%1"=="IIIII" set result=4
if "%1"=="IIIIII" set result=5
if "%1"=="IIIIIII" set result=6
if "%1"=="IIIIIIII" set result=7
if "%1"=="IIIIIIIII" set result=8
if "%1"=="IIIIIIIIII" set result=9
if "%reverse%"=="yes" set num=%result%%num%
if not "%reverse%"=="yes" set num=%num%%result%
shift
if not "%1"=="" goto valloop1
set result=
set reverse=
goto fin
:noarg
echo look , how do you expect me to get your number ? i need a ARGUMENT to run
pause
goto fin
:nochoice
echo missing choice.com, it's surely because you on an NT machine, stupid microsoft
echo you can pick a win9x's choice.com, it must work
echo to use his batch file you will have to use the following command line :
echo %comspec% /e:32768 /c EVENODD nochoice c d u
echo exemples :
echo evenodd nochoice 1 0
echo evenodd nochoice 1 2 3
echo evenodd nochoice 2 5 6
goto fin
:addone
set loop=
set one=I
:addonel
shift
set result=%1%one%
set one=
if "%result%"=="IIIIIIIIIII" set one=I
if "%result%"=="IIIIIIIIIII" set result=I
if not "%loop%"=="" set loop=%loop% %result%
if "%loop%"=="" set loop=%result%
if not "%2"=="" goto addonel
if "%one%"=="I" set loop=%loop% II
"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
awww. just spent a whole 15 (yes 15) mins doing it in dark basic to find out someone else has done it. thatll teach me to read down the whole thread. anyway this is what i got.
x#=10
rem - number
n#=1
rem - count
while x#>1
n#=n#+1
print x# : print " ";
xtwo#=x#/2
xtre#=int(xtwo#)
if (xtre#=xtwo#)=0
x#=x#/2
else
x#=3*x#+1
endif
endwhile
Here's my contribution...hopefully it is interesting and enlightening. I tried to do a language different than the ones that had already been tackled:
PROLOG
next(Num,Nextnum) :- Num>0, X is mod(Num,2), X is 0, Nextnum is Num//2, write(Num), nl.
next(Num,Nextnum) :- Num>0, X is mod(Num,2), X is 1, Nextnum is 3*Num+1, write(Num), nl.
prog(Num,Iter) :- Num>1, next(Num,Nextnum), prog(Nextnum,I), Iter is I+1.
prog(1,1) :- write(1).
The solution is wonderfully succinct. There are four rules. The first tests whether the first number is even and returns the next number. The second tests whether the first number is odd and returns the next number. If the number is less than 0, both rules will automatically return false. The next rule is given a starting number and iterates until the fourth rule, the base case, is true.
So if I were to type "prog(10,X).", the program would return:
10
5
16
8
4
2
1
X = 7 ;
no (this signifies that there aren't multiple answers, which is good )
So there you go. I really like Prolog, it requires you to think a little differently.
"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
I thought I'd see how this thread was doing, and I also thought I'd add another language or two to the list. Here's a solution in Scheme, compiled and tested:
To someone who isn't familiar, and especially when seen in its non-indented state, all those parentheses must seem pretty scary. The periods at the beginning of every line mean nothing, I just put them there to add much-needed indentation. I threw in a helper function so I could keep a running count of the number of steps to reach the goal...if you consider that count unnecessary, then the code is a little shorter and more straightforward. The code can be run after compilation by typing (prob 10) or (prob 143) or whatever.
Now, since I wrote the Scheme code, and since Scheme is a dialect of Lisp, I might as well put the Lisp code here too. It's very similar, but unfortunately, I don't have a Lisp compiler on this computer, so it remains untested. As I am new to Lisp, there very well could be a problem:
(defun prob
....(labels ((helper (n counter)
................(format t "~A~%" n)
................(cond
..................((= n 1)
...................(format t "Reached goal in ~A steps~%" counter))
..................((even n)
...................(helper (/ n 2) (+ 1 counter)))
..................(t
...................(helper (+ (* 3 n) 1) (+ 1 counter))))))
......(helper n 1)))
With all the built-in functions that Common Lisp has, I wouldn't be surprised if there's one out there that could do the recursive counting for me, but I don't know what it is and don't want to take the time to look.