Challenge: Algorithms having only one line of code. Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 06-24-2003, 10:21 PM Oh, you want to compute the factorial of a number. Well, for the moment, I can't see how you can do that in one line of code, without a function of course. Normally a factorial is computed using a FOR loop. That doesn't mean that it can't be done. Keep trying. ***** Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 06-25-2003, 10:43 PM Here's a little algorithm that I derived. Given a positive integer number N, compute the next multiple of X: Result to M. defint a-z M = int((N+X)/X)*X Note: "next" multiple means that if you're already at a multiple, you move up to the next multiple. Example: if N=5 and X=5, the result will be 10. If you got that, then make a slight change to the algorithm to compute the "nearest" multiple. Example: if n=5 and x=5, the result is 5 because it is the nearest (you're already there). This is like rounding --- if it's already rounded, then that's the answer. ***** Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 06-25-2003, 11:30 PM Factorial in one line? Code:`function fact&(x&):if x& then fact&=x&*fact&(x&-1) else fact&=1 :end function` Antoni Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 06-26-2003, 12:16 AM Wow, Antoni! A recursive call to itself --- very sofisticated! Let me try it out . ***** Scolta noi ------------- it works! Brilliant, Antoni. ***** Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 06-26-2003, 02:12 AM I have not tested it, but I think it does'nt work. END FUNCTION can't be in the same line of a single line IF... Antoni Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 06-26-2003, 02:33 AM Antoni, What I tested was the your one line of code in between a standard Function definition line and a standard End Function line. It's still a one line function. ***** Antoni Gual Posting Freak Posts: 1,407 Threads: 117 Joined: Dec 2002 06-26-2003, 02:55 AM Thanks for considering it like this! Antoni Agamemnus Posting Freak Posts: 3,368 Threads: 195 Joined: Jan 2003 06-26-2003, 06:15 AM HEY! NO FAIR! IT's THREE LINES OF CODE! Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war." Visit www.neobasic.net to see rubbish in all its finest. seph Posting Freak Posts: 1,813 Threads: 284 Joined: Mar 2003 06-26-2003, 09:57 AM I don't understand what that function does or what a factoral is. Please explain EVERYTHING IN THE UNIVERSE to me. Nevermind. Some jerk named dictionary.com very rudely explained it to me... earn. na_th_an Posting Freak Posts: 6,419 Threads: 74 Joined: Mar 2002 06-26-2003, 05:59 PM Well, in the line of Antoni's, here's my very own fibonnaci series callculator: Code:```FUNCTION fibonacci& (n&)    IF n& < 2 THEN fibonacci& = 1 ELSE fibonacci& = fibonacci&(n& - 1) + fibonacci&(n& - 2) END FUNCTION``` You use it this way (for example): Code:```FOR i& = 0 TO 10    PRINT fibonacci(i&) NEXT``` SCUMM (the band) on Myspace! ComputerEmuzone Games Studio underBASIC, homegrown musicians [img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i « Next Oldest | Next Newest »