Author Topic: How to find a Integer number's prime root and exponent  (Read 16399 times)

0 Members and 1 Guest are viewing this topic.

chlh_jd

  • Guest
Re: How to find a Integer number's prime root and exponent
« Reply #30 on: October 16, 2012, 11:11:58 AM »
old
Code - Auto/Visual Lisp: [Select]
  1. (defun eea (n)
  2.   (defun f (n x y)
  3.     (cond ((> 1 n) nil)
  4.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  5.           ((> y 0) (cons (list x y) (f n 2 0)))
  6.           ((> x n) nil)
  7.           ((f n (+ 1 x (rem x 2)) 0))
  8.     )
  9.   )
  10.   (f n 2 0)
  11. )
Nice code , Evgeniy  :-)
Thanks Lee Mac and ribarm for your participation .

Hi Evgeniy , After I see  your first version , It's so Concise . so I change it and test
Code - Auto/Visual Lisp: [Select]
  1. (defun eea (n / f)
  2.   (defun f (n x y)
  3.     (cond ((and (> y 0) (zerop (1- n))) (list x y))
  4.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  5.           ((and (> (rem n x) 0) (> y 0)) nil)
  6.           ((< 1 (sqrt n) x) (list n 1))  
  7.           ((f n (+ 1 x (rem x 2)) 0))
  8.     )
  9.   )
  10.   (f n 2 0)
  11. )
  12.  

Now , Test efficiency of  All functions , I don't sure the Quickbench function is correct ( If like Kerry said it perhaps must be improve some . )
Code: [Select]
;;Quickbench copy from http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
(QuickBench (mapcar '(lambda (f) (list f 19487171)) (list SF:pf LM:pf VK:pf SF:pf2 SS:pf  eea)))
;;
Benchmarking ...... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a1e7ea8 SF:PF2> 1...)     32768      1950      1950      1.95
(#<USUBR @000000002a0b3480 SS:PF> 19...)     16384      1060      2120      1.80
(#<USUBR @000000002a2ca200 EEA> 1948...)     16384      1092      2184      1.74
(#<USUBR @000000002a1e74f8 SF:PF> 19...)     16384      1107      2214      1.72
(#<USUBR @000000002a1e75e8 VK:PF> 19...)     16384      1639      3278      1.16
(#<USUBR @000000002a1e7728 LM:PF> 19...)     16384      1904      3808      1.00
--------------------------------------------------------------------------------

Code: [Select]
;;Quickbench copy from http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
;;
(QuickBench (mapcar '(lambda (f) (list f 48828125)) (list SF:pf LM:pf VK:pf SF:pf2 SS:pf  eea)))
;;
Benchmarking ...... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a0b3480 SS:PF> 48...)     32768      1701      1701      2.57
(#<USUBR @000000002a1e7ea8 SF:PF2> 4...)     32768      1842      1842      2.37
(#<USUBR @000000002a2ca200 EEA> 4882...)     16384      1061      2122      2.06
(#<USUBR @000000002a1e74f8 SF:PF> 48...)     16384      1248      2496      1.75
(#<USUBR @000000002a1e75e8 VK:PF> 48...)     16384      1450      2900      1.50
(#<USUBR @000000002a1e7728 LM:PF> 48...)      8192      1091      4364      1.00
--------------------------------------------------------------------------------
« Last Edit: October 16, 2012, 11:17:00 AM by chlh_jd »

chlh_jd

  • Guest
Re: How to find a Integer number's prime root and exponent
« Reply #31 on: October 16, 2012, 11:15:27 AM »
I think this condition as Lee Mac OPT Setfan's (< 1 (sqrt n) (setq x (if (= x 2) 3 (+ 2 x)))) reduce a lot of calculation .
 
« Last Edit: October 16, 2012, 11:20:25 AM by chlh_jd »

chlh_jd

  • Guest
Re: How to find a Integer number's prime root and exponent
« Reply #32 on: October 16, 2012, 11:35:54 AM »
If test number is 399880009 , mine didn't get the result . another test for 399880009
It's a very different results :lol:
Code: [Select]
_$
Benchmarking ..... done for 128 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a1e7728 LM:PF> 39...)       128      1436      1436      2.37
(#<USUBR @000000002a1e75e8 VK:PF> 39...)        64      1106      2212      1.54
(#<USUBR @000000002a1e74f8 SF:PF> 39...)        64      1325      2650      1.28
(#<USUBR @000000002a2ca200 EEA> 3998...)        64      1575      3150      1.08
(#<USUBR @000000002a1e7ea8 SF:PF2> 3...)        64      1702      3404      1.00
--------------------------------------------------------------------------------
 
_$

Use my test function result
Code: [Select]
(ss-test 64 '((SF:pf 399880009) (LM:pf 399880009) (VK:pf 399880009) (SF:pf2 399880009) (eea 399880009)))
Code: [Select]
_$

测试耗时......毫秒 / 相对速度 ...... 迭代次数64...:
 LM:PF......359 / 2.39......<Fastest>
 VK:PF......562 / 1.53......
 SF:PF......702 / 1.22......
 EEA......796 / 1.08......
 SF:PF2......858 / 1.00......<Slowest>
_$

Code: [Select]
(defun ss-test (ti funlist / t1 t2 tilst ilst i)
  ;;时间测试函数,funlist 格式为 ((test1 arg) (test2 arg) (test3 arg))
  ;;GSLS(SS) 2011-5-21
  (foreach funname funlist
    (gc)
    (setq t1 (getvar "MilliSecs"))
    (repeat ti
      (vl-catch-all-apply
(car funname)
(cdr funname)
      )
    )
    (setq t2 (getvar "MilliSecs")
  tilst (append tilst (list(- t2 t1)))
  )
  )
  (setq ilst (vl-sort-i tilst (function <)))
  (princ (strcat "\n测试耗时......毫秒 / 相对速度 ...... 迭代次数" (rtos ti 2 0) "...:"))
  (foreach i ilst
    (princ (strcat "\n "
   (vl-prin1-to-string
     (car (nth i funlist))
     )
     "......"      
     (rtos (nth i tilst) 2 0)
     " / "
     (rtos (/ (nth (last ilst) tilst) (nth i tilst)) 2 2)
     "......"
   (cond ((= i (car ilst)) "<Fastest>")
((= i (last ilst)) "<Slowest>")
(t ""))
   )
   )
    ) 
  (princ)
)
« Last Edit: October 16, 2012, 11:44:03 AM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to find a Integer number's prime root and exponent
« Reply #33 on: October 16, 2012, 12:15:24 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:PF3 ( n / d l x )
  2.     (setq d 0)
  3.     (while (zerop (rem n 2))
  4.         (setq d (1+ d)
  5.               n (/ n 2)
  6.         )
  7.     )
  8.     (if (< 0 d)
  9.         (setq l (cons (list 2 d) l))
  10.     )
  11.     (setq x 3)
  12.     (while (< 1 n)
  13.         (while (/= 0 (rem n x))
  14.             (setq x (+ x 2))
  15.         )
  16.         (setq d 0)
  17.         (while (zerop (rem n x))
  18.             (setq d (1+ d)
  19.                   n (/ n x)
  20.             )
  21.         )
  22.         (setq l (cons (list x d) l))
  23.     )
  24.     (reverse l)
  25. )

 :-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to find a Integer number's prime root and exponent
« Reply #34 on: October 16, 2012, 05:08:32 PM »
old
Code - Auto/Visual Lisp: [Select]
  1. (defun eea (n)
  2.   (defun f (n x y)
  3.     (cond ((> 1 n) nil)
  4.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  5.           ((> y 0) (cons (list x y) (f n 2 0)))
  6.           ((> x n) nil)
  7.           ((f n (+ 1 x (rem x 2)) 0))
  8.     )
  9.   )
  10.   (f n 2 0)
  11. )
Nice code , Evgeniy  :-)
Thanks Lee Mac and ribarm for your participation .

Hi Evgeniy , After I see  your first version , It's so Concise . so I change it and test
Code - Auto/Visual Lisp: [Select]
  1. (defun eea (n / f)
  2.   (defun f (n x y)
  3.     (cond ((and (> y 0) (zerop (1- n))) (list x y))
  4.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  5.           ((and (> (rem n x) 0) (> y 0)) nil)
  6.           ((< 1 (sqrt n) x) (list n 1))  
  7.           ((f n (+ 1 x (rem x 2)) 0))
  8.     )
  9.   )
  10.   (f n 2 0)
  11. )
  12.  

Now , Test efficiency of  All functions , I don't sure the Quickbench function is correct ( If like Kerry said it perhaps must be improve some . )
Code: [Select]
;;Quickbench copy from http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
(QuickBench (mapcar '(lambda (f) (list f 19487171)) (list SF:pf LM:pf VK:pf SF:pf2 SS:pf  eea)))
;;
Benchmarking ...... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a1e7ea8 SF:PF2> 1...)     32768      1950      1950      1.95
(#<USUBR @000000002a0b3480 SS:PF> 19...)     16384      1060      2120      1.80
(#<USUBR @000000002a2ca200 EEA> 1948...)     16384      1092      2184      1.74
(#<USUBR @000000002a1e74f8 SF:PF> 19...)     16384      1107      2214      1.72
(#<USUBR @000000002a1e75e8 VK:PF> 19...)     16384      1639      3278      1.16
(#<USUBR @000000002a1e7728 LM:PF> 19...)     16384      1904      3808      1.00
--------------------------------------------------------------------------------

Code: [Select]
;;Quickbench copy from http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
;;
(QuickBench (mapcar '(lambda (f) (list f 48828125)) (list SF:pf LM:pf VK:pf SF:pf2 SS:pf  eea)))
;;
Benchmarking ...... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a0b3480 SS:PF> 48...)     32768      1701      1701      2.57
(#<USUBR @000000002a1e7ea8 SF:PF2> 4...)     32768      1842      1842      2.37
(#<USUBR @000000002a2ca200 EEA> 4882...)     16384      1061      2122      2.06
(#<USUBR @000000002a1e74f8 SF:PF> 48...)     16384      1248      2496      1.75
(#<USUBR @000000002a1e75e8 VK:PF> 48...)     16384      1450      2900      1.50
(#<USUBR @000000002a1e7728 LM:PF> 48...)      8192      1091      4364      1.00
--------------------------------------------------------------------------------


why fix my code?
Code - Auto/Visual Lisp: [Select]
  1. (defun eea-edit-chlh_jd (n / f)
  2.   (defun f (n x y)
  3.     (cond ((and (> y 0) (zerop (1- n))) (list x y))
  4.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  5.           ((and (> (rem n x) 0) (> y 0)) nil)
  6.           ((< 1 (sqrt n) x) (list n 1))
  7.           ((f n (+ 1 x (rem x 2)) 0))
  8.     )
  9.   )
  10.   (f n 2 0)
  11. )
  12. (defun eea (n)
  13.   (defun f (n x y)
  14.     (cond ((> 1 n) nil)
  15.           ((zerop (rem n x)) (f (/ n x) x (1+ y)))
  16.           ((> y 0) (cons (list x y) (f n 2 0)))
  17.           ((> x n) nil)
  18.           ((f n (+ 1 x (rem x 2)) 0))
  19.     )
  20.   )
  21.   (f n 2 0)
  22. )

Code - Auto/Visual Lisp: [Select]
  1. (BenchMark '((eea 399880009)(eea-edit-chlh_jd 399880009)))
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking .........Elapsed milliseconds / relative speed for 64 iteration(s):
  2.  
  3.     (EEA 399880009)..................1264 / 1.42 <fastest>
  4.     (EEA-EDIT-CHLH_JD 399880009).....1794 / 1 <slowest>
  5.  

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to find a Integer number's prime root and exponent
« Reply #35 on: October 16, 2012, 05:36:45 PM »
code to compare the speed  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun eea-2 (n / x y)
  2.   (setq x 2 y 0)
  3.   (while (and (< x n) (< 0 (rem n x)))
  4.     (setq x (+ 1 x (rem x 2)))
  5.   )
  6.   (while (= 0 (rem n x))
  7.     (setq n (/ n x) y (1+ y))
  8.   )
  9.   (if (= 1 n)
  10.     (list x y)
  11.   )
  12. )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to find a Integer number's prime root and exponent
« Reply #36 on: October 16, 2012, 05:45:10 PM »
code to compare the speed  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun eea-2 (n / x y)
  2.   (setq x 2 y 0)
  3.   (while (and (< x n) (< 0 (rem n x)))
  4.     (setq x (+ 1 x (rem x 2)))
  5.   )
  6.   (while (= 0 (rem n x))
  7.     (setq n (/ n x) y (1+ y))
  8.   )
  9.   (if (= 1 n)
  10.     (list x y)
  11.   )
  12. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (eea-2 18)
  2. nil
  3. _$ (LM:PF3 18)
  4. ((2 1) (3 2))

We are not comparing apples with apples...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to find a Integer number's prime root and exponent
« Reply #37 on: October 16, 2012, 05:48:22 PM »
Code: [Select]
(Foo 144) --> NIL ;;(expt 12 2) = 144 , But 12 is not Prime Number .
see first post...

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to find a Integer number's prime root and exponent
« Reply #38 on: October 16, 2012, 06:02:22 PM »
Code: [Select]
(Foo 144) --> NIL ;;(expt 12 2) = 144 , But 12 is not Prime Number .
see first post...

In that case:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( n / d x )
  2.     (if (zerop (rem n 2))
  3.         (setq x 2)
  4.         (progn
  5.             (setq x 3)
  6.             (while (< 0 (rem n x)) (setq x (+ x 2)))
  7.         )
  8.     )
  9.     (setq d 0)
  10.     (while (zerop (rem n x)) (setq n (/ n x) d (1+ d)))
  11.     (if (= 1 n)
  12.         (list x d)
  13.     )
  14. )

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: How to find a Integer number's prime root and exponent
« Reply #39 on: October 16, 2012, 07:04:45 PM »
399880009 is my function's favourite number  :-P

chlh_jd

  • Guest
Re: How to find a Integer number's prime root and exponent
« Reply #40 on: October 17, 2012, 01:56:48 AM »
Hi Evgeniy ,
Hi Lee Mac ,
I'm sorry for my poor english .
In my first post , I supposed the condition : the number only one factor , the number 144 has two factors so it return nil .

399880009 is my function's favourite number  :-P
It's the max powered number  in my computer . :-)

chlh_jd

  • Guest
Re: How to find a Integer number's prime root and exponent
« Reply #41 on: October 17, 2012, 02:06:56 AM »
New Test
Code: [Select]
(QuickBench (mapcar '(lambda (f) (list f 399880009)) (list SF:pf LM:pf VK:pf SF:pf2  eea eea-2 LM:f2)))
Code: [Select]
Benchmarking ....... done for 128 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a4aab10 LM:F2> 39...)       128      1077      1077      3.22
(#<USUBR @000000002a4aa1d8 LM:PF> 39...)       128      1388      1388      2.50
(#<USUBR @000000002a620a20 EEA-2> 39...)       128      1902      1902      1.82
(#<USUBR @000000002a3c7b38 VK:PF> 39...)        64      1107      2214      1.56
(#<USUBR @000000002a3c75e8 SF:PF> 39...)        64      1358      2716      1.28
(#<USUBR @000000002a6207a0 EEA> 3998...)        64      1560      3120      1.11
(#<USUBR @000000002a6203e0 SF:PF2> 3...)        64      1732      3464      1.00
--------------------------------------------------------------------------------
For 19487171
Code: [Select]
(QuickBench (mapcar '(lambda (f) (list f 19487171)) (list SF:pf LM:pf VK:pf SF:pf2 SS:pf  eea eea-2 LM:f2)))
Code: [Select]
Benchmarking ........ done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @000000002a4aab10 LM:F2> 19...)     32768      1482      1482      2.55
(#<USUBR @000000002a620a20 EEA-2> 19...)     32768      1592      1592      2.37
(#<USUBR @000000002a6203e0 SF:PF2> 1...)     32768      1887      1887      2.00
(#<USUBR @0000000029f2c638 SS:PF> 19...)     32768      2011      2011      1.88
(#<USUBR @000000002a3c75e8 SF:PF> 19...)     16384      1044      2088      1.81
(#<USUBR @000000002a6207a0 EEA> 1948...)     16384      1046      2092      1.80
(#<USUBR @000000002a3c7b38 VK:PF> 19...)     16384      1638      3276      1.15
(#<USUBR @000000002a4aa1d8 LM:PF> 19...)     16384      1887      3774      1.00
--------------------------------------------------------------------------------
« Last Edit: October 17, 2012, 02:11:04 AM by chlh_jd »

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: How to find a Integer number's prime root and exponent
« Reply #42 on: October 17, 2012, 02:54:58 AM »
It's the max powered number  in my computer . :-)
my function will root even bigger numbers i. e. 40000000000 (200000 ^ 2)

New Test
Code: [Select]
(QuickBench (mapcar '(lambda (f) (list f 399880009)) (list SF:pf LM:pf VK:pf SF:pf2  eea eea-2 LM:f2)))
i have different results, and i can't understand why...
Code: [Select]
(QuickBench
  (mapcar '(lambda (f) (list f 399880009))
  (list VK:pf eea-2 LM:f2)
  )
)
Code: [Select]
Benchmarking ... done for 4096 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(#<USUBR @0822a9c4 VK:PF> 399880009)          4096      1080      1080    136.89
(#<USUBR @0822ad0c LM:F2> 399880009)            64      1313     84032      1.76
(#<USUBR @0822ace4 EEA-2> 399880009)            32      1155    147840      1.00
--------------------------------------------------------------------------------
« Last Edit: October 17, 2012, 03:21:00 AM by VovKa »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to find a Integer number's prime root and exponent
« Reply #43 on: October 17, 2012, 03:47:39 AM »
Vovka, your routine
Code - Auto/Visual Lisp: [Select]
  1.  (vk:pf 399880009)

blows up my computer :-D
with acad2013 64 win 7 pro
Quote
Command: (vk:pf 399880009)
Hard error occurred ***
internal stack limit reached (simulated)

However, if I use 48828125 I get this result
Code - Auto/Visual Lisp: [Select]
  1. (defun vk:pf (n / a b c)
  2.   ;;by VovKa
  3.   (setq a (1+ (fix (/ (log n) (log 2)))))
  4.   (while (setq a (1- a)
  5.                c (expt n (/ 1. a))
  6.                b (fix (+ c 0.5))
  7.                c (not (equal (/ c b) 1 1e-8))
  8.          )
  9.   )
  10.   (if (is-prime b)
  11.     (list b a)
  12.   ) ;_add is-prime
  13. )
  14. (defun is-prime (n / foo)
  15.   ;;by VovKa
  16.   (defun foo (n try)
  17.     (if (= n try)
  18.       t
  19.       (if (zerop (rem n try))
  20.         nil
  21.         (foo n (+ try 1))
  22.       )
  23.     )
  24.   )
  25.   (foo n 2)
  26. )
  27.  
  28.  
  29. (defun eea-2 (n / x y)
  30.   ;; ElpanovEvgeniy http://www.theswamp.org/index.php?topic=42965.msg482261#msg482261
  31.   (setq x 2
  32.         y 0
  33.   )
  34.   (while (and (< x n) (< 0 (rem n x)))
  35.     (setq x (+ 1 x (rem x 2)))
  36.   )
  37.   (while (= 0 (rem n x))
  38.     (setq n (/ n x)
  39.           y (1+ y)
  40.     )
  41.   )
  42.   (if (= 1 n)
  43.     (list x y)
  44.   )
  45. )
  46.  
  47. (defun lm:f2 (n / d x)
  48.   ;; LM  http://www.theswamp.org/index.php?topic=42965.msg482268#msg482268
  49.   (if (zerop (rem n 2))
  50.     (setq x 2)
  51.     (progn (setq x 3)
  52.            (while (< 0 (rem n x)) (setq x (+ x 2)))
  53.     )
  54.   )
  55.   (setq d 0)
  56.   (while (zerop (rem n x))
  57.     (setq n (/ n x)
  58.           d (1+ d)
  59.     )
  60.   )
  61.   (if (= 1 n)
  62.     (list x d)
  63.   )
  64. )

Code - Auto/Visual Lisp: [Select]
  1. (benchmark
  2.   '((vk:pf 48828125)
  3.     (eea-2 48828125)
  4.     (lm:f2 48828125))
  5. )
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking [M.P. 2005 < revised kdub 2005>] ................
  2. Elapsed milliseconds for 8192 iteration(s)/ relative Timing :
  3.  
  4.     (VK:PF 48828125).....375 / 1.7202 <slowest>
  5.     (EEA-2 48828125).....219 / 1.0046
  6.     (LM:F2 48828125).....218 / 1 <fastest>

I assume that those versions are correct :)
Regards
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: How to find a Integer number's prime root and exponent
« Reply #44 on: October 17, 2012, 04:17:47 AM »
(defun is-prime   (n / foo)
  ;;by VovKa
  (defun foo (n try)
it's not 'by VovKa', it's 'by chlh_jd'

anyway my function should not be benchmarked along with others because it doesn't work as supposed