Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Algorithms having only one line of code.
#45
Ok, let's get back to the topic. We haven't had a one-line algorithm since Oracle's calculation for PI.

A few days ago I posted the following:
Code:
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.
Nobody came up with the method of computing the "nearest" multiple, so here it is:
Code:
M = int((N+X-1)/X)*X
*****
Reply


Messages In This Thread
To Oracle: 2nd look at your code. - by Moneo - 06-24-2003, 09:07 PM
To Seph and Agamemnus: - by Moneo - 06-24-2003, 09:17 PM
factorial. - by Agamemnus - 06-24-2003, 09:51 PM
To Agamemnus - by Moneo - 06-24-2003, 10:21 PM
To na_th_an: - by Moneo - 06-27-2003, 03:15 AM
ok, - by Agamemnus - 06-27-2003, 05:17 PM
To Oracle re Statlib - by Moneo - 06-27-2003, 10:39 PM
Re: To na_th_an: - by na_th_an - 06-28-2003, 01:14 AM
To LooseCaboose: - by Moneo - 06-28-2003, 03:08 AM
Re: To Oracle re Statlib - by oracle - 06-28-2003, 05:50 AM
To Oracle: - by Moneo - 06-29-2003, 03:10 AM
To Ak00ma: - by Moneo - 06-30-2003, 03:39 AM
Challenge: Algorithms having only one line of code. - by Moneo - 06-30-2003, 07:35 AM
Power of 2 - by Moneo - 07-02-2003, 02:42 AM
To Antoni: - by Moneo - 07-02-2003, 03:03 AM
197 - by whitetiger0990 - 07-02-2003, 06:29 PM
To na_th_an re "replacement" logic: - by Moneo - 07-02-2003, 08:29 PM
Re ROUNDING using .5: - by Moneo - 07-02-2003, 08:47 PM
Re: Re ROUNDING using .5: - by Moneo - 03-20-2006, 06:19 AM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:09 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:24 PM
Challenge: Algorithms having only one line of code. - by Anonymous - 03-29-2006, 10:52 PM
my one-liner - by neuro - 03-30-2006, 12:08 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)