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

Post Reply  Post Oekaki 
 

Posted By Message

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!!!
   

Post Reply



 



Advertisement

Worth A Click