The Daily Click ::. Forums ::. Non-Klik Coding Help ::. 3x+1
 

Post Reply  Post Oekaki 
 

Posted By Message

Shen

Possibly Insane

Registered
  14/05/2002
Points
  3497
20th April, 2004 at 16:56:37 -

* 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; }


Image Edited by the Author.

 
gone fishin'

Shen

Possibly Insane

Registered
  14/05/2002
Points
  3497
20th April, 2004 at 17:33:22 -

I thought I did at the start

Take x to be a number - usually 10. If x is even, halve it. If it is odd, triple it, then add one. For example:

10 is even - halves to 5
5 is odd - 3x+1 to 16
16 even - 8
8 even - 4
4 even - 2
2 even - 1

Here, 1 is reached in 7 steps. My program (the Javascript one) does this conjecture, and prints out its results until it gets to 1.

 
gone fishin'

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 17:37:32 -

C:

#include <stdio.h>

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;
}



Image 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

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 17:43:14 -

C++, not much difference:

#include <iostream>
using namespace std;

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

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
20th April, 2004 at 18:01:31 -

Perl (console output)

$x=10; # number

$n=1; # count

while(x>1) {
n++;
print $x." ";
if ($x%2 == 0) $x /= 2;
else $x = 3*$x+1;
}

print "1\n(in ".$n." steps)");


Perl (CGI)

#!/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>";

$x=10; # number
$n=1; # count

while(x>1) {
n++;
print $x." ";
if ($x%2 == 0) $x /= 2;
else $x = 3*$x+1;
}

print "1
(in ".$n." steps)</body></html>");


PHP

$x=10; // number

$n=1; // count

while(x>1) {
n++;
print $x." ";
if ($x%2 == 0) $x /= 2;
else $x = 3*$x+1;
}

print "1
(in ".$n." steps)");



Spot the difference, people.

Image Edited by the Author.

 
n/a

Kramy



Registered
  08/06/2002
Points
  1888
21st April, 2004 at 01:09:20 -

It's almost identical for Jamascript. My coding habits are slightly different though, so I wrote it different.

Jamascript:

// Make window for Result
myWindow = New Window(320,240);

Var N = 10; // Initial Number
Var Loops = 1;

For(;
{
// Increase count
Loops++;
// Write output
myWindow.Write(N+" ");
// Get Next Number
N = Next;

If(N == 1)
{
myWindow.WriteLn("1");
Break;
}
}

myWindow.WriteLn("One was reached in "+Loops+" loops");

Function Next(pNum)
{
If(pNum%2 == 0) Return pNum/2;
Else Return 3*pNum+1;
}

While(1);


Exact Conversion:

// Make window for Result
myWindow = New Window(320,240);

Var x = 10; // The number
Var n = 1; // The count

While(x > 1)
{
n++; // Increase count

myWindow.WriteLn(x+" "); // Output x

x = Next(x); // Change x
}

myWIndow.WriteLn("1
(in "+n+" steps)");

While(1);

Function Next(pNum) {If(pNum%2 == 0) Return pNum/2; Return 3*pNum+1;}



Image Edited by the Author.

 
Kramy

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
21st April, 2004 at 02:52:05 -

Yay! Someone else who likes Perl * ShadowCaster gives CBFM a *hug*

Psuedocode:
Nah, I'm just kidding, psuedocode sucks

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Ashman

Possibly Insane

Registered
  12/06/2002
Points
  3974
21st April, 2004 at 03:14:20 -

Oh - my - god...

 
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.


"Outshined" - SoundGarden

Batchman



Registered
  08/08/2003
Points
  231
21st April, 2004 at 05:00:51 -

python

x,n=10,1

while x>1:
print x
x,n=int(x*(0.5+((x&1)*2.5)))+(x&1),n+1
print "1\nachieved in", n,"step"+((n>1)*"s")



 
n/a

taco ( Crobasoft )



Registered
  04/04/2004
Points
  131
22nd April, 2004 at 20:17:14 -

ahah. python is like the c++ wannabe.

 
42

Mark Beazley



Registered
  02/01/2002
Points
  766
28th April, 2004 at 17:10:30 -

I made this in MMF in 2001 I will try to find it out tommorow.

 
www.gethyper.co.uk

Mr Icekirby



Registered
  18/12/2003
Points
  846
28th April, 2004 at 17:41:54 -

how about basic? anyone use basic anymore?

 
Mr Icekirby says so!
OBEY ME!

RapidFlash

Savior of the Universe

Registered
  14/05/2002
Points
  2712
28th April, 2004 at 18:03:11 -

Visual Basic

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

Image Edited by the Author.

 
http://www.klik-me.com

Galaxy613



Registered
  29/01/2003
Points
  1765
28th April, 2004 at 20:20:53 -

int next(int n) { if (n%2==0) return n >> 1; else return n*3+1; }


could also be

#define next(x) (n%2==0 ? n >> 1 : n*3+1)


Image Edited by the Author.

 
Image
My forum: http://subsoap.com/ck/forums/index.php

David Newton (DavidN)

Invisible

Registered
  27/10/2002
Points
  8322

Honored Admin Alumnus
29th April, 2004 at 05:56:21 -

Commodore BASIC!

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.

 
http://www.davidn.co.nr - Games, music, living in America

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
29th April, 2004 at 12:18:20 -

Wow, ages since I've seen that code

Psion OPL (like any of you know what that is)


PROC MAIN:
LOCAL x%
LOCAL n%
x% = 10
n% = 1

WHILE x% > 1
n% = n% + 1
PRINT NUM$(x% + 1)
IF (x%+1)/2 = x%/2
x% = x% / 2
ELSE
x% = 3 * x% + 1
ENDIF
ENDWH

PRINT "1"
PRINT " in " + NUM$( x%, 3 ) + " steps"
GET
ENDP



 
n/a

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
29th April, 2004 at 17:25:48 -

TI-83 BASIC:


10 -> X
disp X

While X > 1

X -> A
0 -> B

while A > 0

B+1 -> B
A-1 -> A
if B >= 2 : 0 -> B

End

If B = 0: X/2 -> X
If B = 1: 3X+1 -> X
disp X

End


Image Edited by the Author.

 
n/a

Mark Beazley



Registered
  02/01/2002
Points
  766
29th April, 2004 at 18:31:54 -

OK found my MMF 1.5 version and spruced it up. 199999994 is the largest starting number it can handle.

Uses the fast loop object, and the window object (both from cellosoft)

Download the CCA here: http://www.gethyper.co.uk/dl/3n+1.cca

 
www.gethyper.co.uk

Mr Icekirby



Registered
  18/12/2003
Points
  846
29th April, 2004 at 20:51:41 -

CK4R1, what is that for? which language are you coding in? and i didn't mean visual basic, i meant basic, you know, the first? basic is very old

 
Mr Icekirby says so!
OBEY ME!

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
29th April, 2004 at 20:57:00 -

CK4R1 is simplifying part of the C/C++ source code that Kris posted.

 
n/a

Lew



Registered
  06/01/2002
Points
  1014
30th April, 2004 at 07:51:29 -

DarkBASIC (yes I suck)

Edit: Woops. DarkBASIC pro needs #'s for real numbers...


input "insert number", x#

do


if x#/2 = int(x#/2) and x# <> 1
x# = x#/2
print x#

else if x# <> 1
x# = (x# * 3) + 1
print x#
endif
endif

loop


Image Edited by the Author.

 
<--intelligent, witty comment here-->

Batchman



Registered
  08/08/2003
Points
  231
30th April, 2004 at 09:37:05 -

this algoritm doesn't need real number , we olny divide a even number by two

btw i'm making a batch version of this

 
n/a

Lew



Registered
  06/01/2002
Points
  1014
30th April, 2004 at 10:28:47 -

It does because otherwise my formula doesn't know whether it's even (as it checks real division against integer division) so it just halves it.

 
<--intelligent, witty comment here-->

Batchman



Registered
  08/08/2003
Points
  231
30th April, 2004 at 13:33:52 -

no it don't , because you have the mod function in db pro

input "insert number", x

do
if x=1 then end
if x mod 2
x=(3*x)+1
else
x=x/2
endif
loop


 
n/a

Shen

Possibly Insane

Registered
  14/05/2002
Points
  3497
30th April, 2004 at 13:52:12 -

Inform ( www.inform-fiction.org )

[ Next n;
if (n%2 == 0) n=n/2;
else n=(n*3)+1;
return n;
];

[ Main x n;
x = 10; n = 0;
while (x > 1) {
x = next(x);
print x; new_line; }
];


 
gone fishin'

Batchman



Registered
  08/08/2003
Points
  231
30th April, 2004 at 16:27:30 -

batch (warning this is real 1337 5P46H377| C0[)3)

@echo off
set thispath=%0

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

:evenoddt
call %thispath% evenoddtest %cumul%

call %thispath% addone %loop%
call %thispath% valtostr %cumul%
echo %num%

if "%cumul%"=="II" goto tendloop

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

set one=

goto fin
:cheat2

echo so you try to play with this batch file ?

goto fin
:fin


exemple of use :

C:\nicolas\merdier\batch\EVENODD>evenodd 12345678901234567890123456789
12345678901234567890123456789
37037036703703703670370370368
18518518351851851835185185184
9259259175925925917592592592
4629629587962962958796296296
2314814793981481479398148148
1157407396990740739699074074
578703698495370369849537037
1736111095486111109548611112
......
172
86
43
130
65
196
98
49
148
74
37
112
56
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
done in 547 loops
C:\nicolas\merdier\batch\EVENODD>

 
n/a

Kramy



Registered
  08/06/2002
Points
  1888
30th April, 2004 at 17:11:53 -

Batchman..you're amazing!

Now make an RTS out of a bat file.

 
Kramy

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
30th April, 2004 at 18:42:59 -

oh my. x_x

 
"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

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
30th April, 2004 at 20:56:42 -

I can only say: at least Longhorn will have a decent batch file system.

 
n/a

Zane



Registered
  09/09/2003
Points
  1183
30th April, 2004 at 21:09:17 -

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


print "in "+str$(n#)+" steps"


 
www.klikforever.co.uk
GO THERE!!!

Scott Handelman



Registered
  19/05/2004
Points
  117
11th August, 2004 at 01:20:47 -

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.

Image Edited by the Author.

 
n/a

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
12th August, 2004 at 17:55:30 -

Brainf.................... sod that

 
"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

gizmo



Registered
  15/03/2003
Points
  1206
13th August, 2004 at 12:19:02 -

ph34r my pseudo-code (not an actual programming language, but would be good for prototyping)



Get initial number
Calculate result
Display result


Edit; Ok ok, i'll use some C# (its a pretty long winded language, or i'm just a n00b ;P)


using System;

namespace _3x_1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int x = 10;
int count = 1;
x = (System.Convert.ToInt16(Console.ReadLine()));
while (x > 1)
{
x = Class1.doNext(x);
Console.WriteLine("X; {0}", x);
count++;
}
Console.WriteLine("Done in {0} steps.", count);
Console.Read();
}

static int doNext(int n)
{
if (n % 2 == 0)
return n >> 1;
else
return n * 3 + 1;
}
}
}


Image Edited by the Author.

Image Edited by the Author.

 
<signature> err... </signature>

Scott Handelman



Registered
  19/05/2004
Points
  117
2nd October, 2004 at 02:10:13 -

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:

(define prob
..(lambda
....(letrec ((helper
..............(lambda (n counter)
................(printf "~A~%" n)
................(cond
..................((= n 1)
...................(printf "Reached goal in ~A steps~%" counter))
..................((even? n)
...................(helper (/ n 2) (+ 1 counter)))
..................(else
...................(helper (+ (* 3 n) 1) (+ 1 counter)))))))
......(helper n 1))))

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.

Image Edited by the Author.

 
n/a
   

Post Reply



 



Advertisement

Worth A Click