Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#11
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.
*****
Reply
#12
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.
*****
Reply
#13
Factorial in one line?
Code:
function fact&(x&):if x& then fact&=x&*fact&(x&-1) else fact&=1 :end function
Antoni
Reply
#14
Wow, Antoni! A recursive call to itself --- very sofisticated!
Let me try it out .
*****
Scolta noi ------------- it works!
Brilliant, Antoni.

*****
Reply
#15
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
Reply
#16
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.
*****
Reply
#17
Thanks for considering it like this!
Antoni
Reply
#18
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.
Reply
#19
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.
Reply
#20
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)