Author Topic: (Challenge) Closest integer to n  (Read 7384 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: (Challenge) Closest integer to n
« Reply #15 on: February 02, 2010, 01:41:15 PM »
My bet is on VovKa  :wink:  While wins in 9/10 cases...

wizman

  • Bull Frog
  • Posts: 283
Re: (Challenge) Closest integer to n
« Reply #16 on: February 02, 2010, 01:55:33 PM »
Here's my entry:

Code: [Select]
(defun wiz-near (n lst / b c d)
    (setq a (list n n)
          b 1e6
    )
    (mapcar '(lambda (x)
                 (if (<= (setq c (distance a (list n x))) b)
                     (setq b c
                           d x
                     )
                 )
             )
            lst
    )
    d
)

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: (Challenge) Closest integer to n
« Reply #17 on: February 02, 2010, 01:59:00 PM »
Very original Wizman I like it!

This is what I get:

Code: [Select]
Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST133-VOVKA 10 LST).....1263 / 2.77 <fastest>
    (CLOSEST-LEEMAC 10 LST).......1388 / 2.52
    (WIZ-NEAR 10 LST).............1435 / 2.43
    (CLOSEST2-WHDJR 10 LST).......1451 / 2.41
    (NEAREST-CAB 10 LST)..........1497 / 2.33
    (TEST-EVGENIY 10 LST).........1841 / 1.90
    (NEAREST2-CAB 10 LST).........3494 / 1.00 <slowest>

wizman

  • Bull Frog
  • Posts: 283
Re: (Challenge) Closest integer to n
« Reply #18 on: February 02, 2010, 02:09:57 PM »
Thanks Lee, good solution by all too:


edit:  just seen others works on reals too, got to rest now
« Last Edit: February 02, 2010, 02:16:06 PM by wizman »

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: (Challenge) Closest integer to n
« Reply #19 on: February 02, 2010, 02:20:55 PM »
A quick (and poor) attempt to be clever  :wink:

Code: [Select]
#include <iostream>
#include <cstdlib>
using namespace std;

// C++ Attempt!
int main( )
{
  float lst[11] = {45,23,63,5,8,56,74,32,96,144,95}, n=10, a, b;
  int   i;

  a = abs(n-lst[0]);

  for (i=1; i<11; i++)
    if (abs(n-lst[i]) < a) a = lst[i];

  cout << a;
 
  return 0;
}

Have no idea how to time it to compare with the others though...
« Last Edit: February 02, 2010, 02:24:10 PM by Lee Mac »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #20 on: February 02, 2010, 02:48:48 PM »
only , benchmark...

Code: [Select]
(defun test1 (n l )
 (setq l (vl-sort l (function <)))
 (while (< (cadr l) n) (setq l (cdr l)))
 (if (< (abs (- n (car l))) (abs (- n (cadr l))))
  (car l)
  (cadr l)
 )
)

Code: [Select]
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST1 N L)..........1329 / 1.08 <fastest>
    (NEAREST133 N L).....1438 / 1 <slowest>
Stay home. Stay safe. Save lives.

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: (Challenge) Closest integer to n
« Reply #21 on: February 02, 2010, 02:54:29 PM »
Haha! Cheeky trick  :-)  Get rid of the values that just clutter the list  ;-)

VovKa

  • Swamp Rat
  • Posts: 1352
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #22 on: February 02, 2010, 03:02:00 PM »
Code: [Select]
(defun test1 (n l )
 (setq l (vl-sort l (function <)))
 (while (< (cadr l) n) (setq l (cdr l)))
 (if (< (abs (- n (car l))) (abs (- n (cadr l))))
  (car l)
  (cadr l)
 )
)
i just knew that Evgeniy would beat us all :)

VovKa

  • Swamp Rat
  • Posts: 1352
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #23 on: February 02, 2010, 03:04:39 PM »
although...
(test1 5 '(1 2 3))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #24 on: February 02, 2010, 03:12:51 PM »
although...
(test1 5 '(1 2 3))

fix:
Code: [Select]
(defun test1 (n l )
 (setq l (vl-sort l (function <)))
 (while (and (cddr l)(< (cadr l) n)) (setq l (cdr l)))
 (if (< (abs (- n (car l))) (abs (- n (cadr l))))
  (car l)
  (cadr l)
 )
)

comparison
Code: [Select]
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST1 N L)..........1359 / 1.06 <fastest>
    (NEAREST133 N L).....1437 / 1 <slowest>

 
_$
 
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST1 N L)..........1453 / 1.09 <fastest>
    (NEAREST133 N L).....1578 / 1 <slowest>
Stay home. Stay safe. Save lives.

wizman

  • Bull Frog
  • Posts: 283
Re: (Challenge) Closest integer to n
« Reply #25 on: February 02, 2010, 03:16:12 PM »


Code: [Select]
(defun wiz-near2 (n lst / b c d)
    (setq  b 1e6)
    (mapcar (function(lambda (x)
                 (if (< (setq c (abs (- n x ))) b)
                     (setq b c
                           d x
                     )
                 )
             ))
            lst
    )
    d
)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7060
  • AKA Daniel
Re: (Challenge) Closest integer to n
« Reply #26 on: February 02, 2010, 07:40:59 PM »
A quick (and poor) attempt to be clever  :wink:

nice  8-)

Code: [Select]
static bool closesTo10 ( int elem1, int elem2)
 {
   return abs(elem1 - 10) < abs(elem2 -10);
 }
 static void ArxBlockExt_doit(void)
 {
   int a[] = {45,23,63,5,8,56,74,32,96,144,95};
   std::sort(a, a + sizeof(a) / sizeof(a[0]),closesTo10);
   acutPrintf(_T("\n%d"),a[0]);
 }
Retired

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: (Challenge) Closest integer to n
« Reply #27 on: February 02, 2010, 07:46:38 PM »
A quick (and poor) attempt to be clever  :wink:

nice  8-)

Thanks Daniel - but amateur compared to yours....  :-)

VovKa

  • Swamp Rat
  • Posts: 1352
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #28 on: February 03, 2010, 04:47:12 AM »
i had a dream last night :)
Code: [Select]
(defun nearest41 (num lst / p a b)
  (cond ((vl-position num lst) num)
((zerop (setq lst (vl-sort (cons num lst) '<)
     p  (vl-position num lst) ; faster than Evgeniy's when (> p 2)
)
)
(cadr lst)
)
((= (last lst) num) (nth (1- p) lst))
((> (- (setq a (nth (1+ p) lst)) num) (- num (setq b (nth (1- p) lst)))) b)
(t a)
  )
)
Code: [Select]
(setq lst '(10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170)
      num 31 ;and above
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #29 on: February 03, 2010, 04:56:34 AM »
i had a dream last night :)

It was colored dream? :)

Good job VovKa!
Stay home. Stay safe. Save lives.