tag:blogger.com,1999:blog-4011252463639388552.post4764954146087353446..comments2024-03-20T14:03:01.388-04:00Comments on JTB World Blog: AutoLISP reverse problemJTB Worldhttp://www.blogger.com/profile/02249678064305703202noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4011252463639388552.post-64916550685990533352012-02-24T13:37:38.930-05:002012-02-24T13:37:38.930-05:00Heady stuff. :)Heady stuff. :)doughttps://www.blogger.com/profile/14420724316577967254noreply@blogger.comtag:blogger.com,1999:blog-4011252463639388552.post-15384114278829526712012-02-24T11:59:02.427-05:002012-02-24T11:59:02.427-05:00Hi Doug,
Please find what the purpose of the code ...Hi Doug,<br />Please find what the purpose of the code is in the updated post.JTB Worldhttps://www.blogger.com/profile/02249678064305703202noreply@blogger.comtag:blogger.com,1999:blog-4011252463639388552.post-75125616009792922482012-02-21T14:42:56.443-05:002012-02-21T14:42:56.443-05:00I like to format my AutoLisp code like this:
(def...I like to format my AutoLisp code like this:<br /><br />(defun h (n / c) <br /> (cons n <br /> (reverse <br /> (cons 1<br /> (while<br /> (/= 1<br /> (setq n<br /> (if<br /> (=(rem n 2) 0)<br /> (/ n 2)<br /> (+(* 3 n)1)<br /> )<br /> )<br /> )<br /> (setq c(cons n c))<br /> )<br /> )<br /> )<br /> ) <br />)<br /><br />This makes seeing the inner most set of parenthesis easy. And in this case the n passed is divided by 2 and if that equation has a remainder of 0 (zero) then that is the value returned. But if that equation fails the else statement is to then set n to 3 (three) its original value + (plus) 1 (one). Recursively the if and else values of the equations are found until the resulting value equals 1 (one).<br /><br />Each value is added to a list that once the while loop is complete, is added to another list already defined with the value of 1 (one) in the first position. The order of the list is then reversed and added to another list already defined with the original value of n passed.<br /><br />Here are the values this method returns when run with the numbers 1 - 15:<br />(h 1) = (1 4 2 1)<br />(h 2) = (2 1)<br />(h 3) = (3 10 5 16 8 4 2 1)<br />(h 4) = (4 2 1)<br />(h 5) = (5 16 8 4 2 1)<br />(h 6) = (6 3 10 5 16 8 4 2 1)<br />(h 7) = (7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)<br />(h 8) = (8 4 2 1)<br />(h 9) = (9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)<br />(h 10) = (10 5 16 8 4 2 1)<br />(h 11) = (11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)<br />(h 12) = (12 6 3 10 5 16 8 4 2 1)<br />(h 13) = (13 40 20 10 5 16 8 4 2 1)<br />(h 14) = (14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)<br />(h 15) = (15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1)<br />(h 16) = (16 8 4 2 1)<br />(h 17) = (17 52 26 13 40 20 10 5 16 8 4 2 1)<br /><br />The description above is what the method does. But why write this method? What need does solve?<br /><br />I thought maybe it has something to do with prime numbers. The highest number in each result, excluding the original value of n passed, is usually 1 (one) less than a prime number.<br /><br />But then I noticed, like with the result of (h 9) = (9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1) that you could break down the results into groups as follow:<br /> (9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)<br /> ( 28 14 7 )<br /> ( 22 11 )<br /> ( 34 17 )<br /> ( 52 26 13 )<br /> ( 40 20 10 5 )<br /> ( 16 8 4 2 1)<br /><br />Each of the groups starts with a prime number. I was almost ready to state that the method is for finding all the prime number less than the value of n, and their products no great than 3 (three) times n, but there are prime numbers great than n.<br /><br />I give up on what is was written to do.<br /><br />:)<br /><br />Dougdoughttps://www.blogger.com/profile/14420724316577967254noreply@blogger.com