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
   

Post Reply



 



Advertisement

Worth A Click