Jump to content

Collatz conjuncture


ollie

Recommended Posts

Hi all,

 

Anyone intrested in the collatz conjuncture (Half or triple plus one) may enjoy this

 

(vl-load-com)
(defun c:colCountMap( / col ulmit ms)
 (setq ms (vla-get-modelspace(vla-get-activedocument(vlax-get-acad-object))))
 (setq lbound (getreal "\nEnter lower limit"))
 (setq ubound (getreal"\nEnter higher limit"))
 (while(<= lbound ubound)
   (vla-addcircle ms (vlax-3d-point (list (float lbound )(setq col(collatz lbound nil)) 0.0)) 0.25)
   (setq lbound (1+ lbound))
   (if(> col ulimit)
     (setq ulimit col)
   )
 )
 (vla-addline ms(vlax-3d-point (list 0.0 0.0 0.0))(vlax-3d-point(list 0.0 (float ulimit))))
 (vla-addline ms(vlax-3d-point (list 0.0 0.0 0.0))(vlax-3d-point(list (float ubound) 0.0 0.0)))      
)
(defun collatz(lbound flag / stop return)
 (setq stop nil)
 (setq return (list lbound))
 (while (= nil stop)
   (if(> lbound 1 )
     (progn
   (if(= 0 (rem lbound 2))
         (setq return (append return (list (/ lbound 2))))
     (setq return (append return (list (1+(* 3 lbound)))))
   )
   (setq lbound (nth (1- (length return)) return))
     )
     (setq stop t)
   )
 )
 (if(/= t flag)
   (setq return (length return))
 )
 return
)

 

Basically it works out the number of iterations between a user defined range until the starting number reaches it's stopping point (1)

 

Each value is plotted to a scatter graph of sorts

 

cheers

Link to comment
Share on other sites

Interesting Ollie - I love the mathematical threads :)

 

I would be inclined to code it like this:

 

(defun c:colCountMap ( / ms lbound ubound col ulimit )  
 (vl-load-com)
 (setq ms (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))

 (if (and (setq lbound (getint "\nEnter Lower limit: "))
          (setq ubound (getint "\nEnter Upper limit: ")))
   (progn
     (while (<= lbound ubound)
       (vla-put-color 
         (vla-addcircle ms
           (vlax-3d-point (list lbound (setq col (Collatz lbound)) 0.0)) 0.25
         )
         acyellow
       )
       (setq lbound (1+ lbound))

       (if (> col ulimit) (setq ulimit col))
     )
     (mapcar
       (function
         (lambda ( p )
           (vla-put-color
             (vla-AddLine ms (vlax-3D-point '(0. 0. 0.)) (vlax-3D-point p))
             acGreen
           )
         )
       )
       (list (list 0. ulimit 0.) (list ulimit 0. 0.))
     )
   )
 )
 (princ)
)

(defun Collatz ( num )
 (
   (lambda ( i )
     (while (< 1 num) (setq i (1+ i))
       (setq num
         (if (zerop (logand 1 num)) (/ num 2) (1+ (* 3 num)))
       )
     )
     i
   )
   0
 )
)

 

I hope you don't mind :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...