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

Post Reply  Post Oekaki 
 

Posted By Message

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