QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Mathman1@comcast.net on July 07, 2020, 11:08:33 am

Title: Collatz Conjecture
Post by: Mathman1@comcast.net on July 07, 2020, 11:08:33 am
I specifically downloaded QB64 to study (play with) Collatz Conjecture (Explained in Wikipedia). QB64 is very flexible and powerful. I wish QB64 had a scrollable output screen but applaud all its other capabilities. You enter the number under the unsigned integer max (18446744073709551615) (somewhat less or you will error immediately) and you will progress through the Collatz procedure. If the output page was scrollable, each operation would be printed on one line. Since it appears the scrollable feature is not available, I print out the operation number, the resulting number, and the resulting number length and some spaces. I do not know how to use the MOD function for the odd/even test so I divide and multiply by 2 and test for equivalency with the original number. If at any time the next result is going to go above the unsigned integer max, I print error and carriage return but still continue. I do 50 operations at a time and when the series finishes, I print end. My series completion number agrees with the Wikipedia example of maximum completion numbers. QB64 is cool!

Code: QB64: [Select]
  1. m~&& = 18446744073709551615 / 3
  2. 1 INPUT "number: ", a~&&: IF a~&& < 1 THEN 1
  3. n = 0: e = 0
  4. 2 PRINT a~&&; LEN(STR$(a~&&)) - 1;: IF e = 50 THEN INPUT s: e = 0
  5. b~&& = a~&& / 2
  6. c~&& = b~&& * 2
  7. IF a~&& >= m~&& AND c~&& <> a~&& THEN PRINT: PRINT "Error"
  8. IF c~&& = a~&& THEN PRINT " E, ";: a~&& = a~&& / 2: ELSE PRINT " O, ";: a~&& = a~&& * 3 + 1
  9. n = n + 1: e = e + 1: IF a~&& <> 1 THEN GOTO 2
  10. PRINT "1  1  END "; n: GOTO 1
Title: Re: Collatz Conjecture
Post by: FellippeHeitor on July 07, 2020, 11:15:27 am
Hi there. To add a code box, just paste your code between code tags:

[code=qb64]'your code here[/code]

And to get scrollable output, you can use the system's console. Add this to the beginning of your program:

Code: QB64: [Select]

Welcome aboard!
Title: Re: Collatz Conjecture
Post by: bplus on July 07, 2020, 11:22:29 am
@FellippeHeitor

Has he (Mathman1) been allowed access to Samples Gallery?

@Mathman1@comcast.net  code usually has to "cook" in Programs Board unless your real avatar name is @STxAxTIC  ;-))
Title: Re: Collatz Conjecture
Post by: bplus on July 07, 2020, 11:36:25 am
@FellippeHeitor

I can remove this but I'd rather it be moved to Programs where we can discuss Collatz. ✨
Title: Re: Collatz Conjecture
Post by: Mathman1@comcast.net on July 07, 2020, 12:05:34 pm
I am new here. I found this item in the book, The Secret Life of Numbers, Chapter 6 of 50 chapters so there may be more programming examples to come. Please move this to Programs if more appropriate.
Title: Re: Collatz Conjecture
Post by: bplus on July 07, 2020, 12:37:18 pm
@Mathman1@comcast.net

I would move it but I only have permissions for Samples Gallery, kinda curious how you have access but OK you're new :)

You could restart in Programs if you don't mind and I can remove the stuff here? (never done that)
Title: Re: Collatz Conjecture
Post by: FellippeHeitor on July 07, 2020, 12:56:41 pm
Only now I noticed the board we're at. Let us pray odin moves it.
Title: Re: Collatz Conjecture
Post by: FellippeHeitor on July 07, 2020, 12:57:38 pm
Done (amen).
Title: Re: Collatz Conjecture
Post by: Qwerkey on July 07, 2020, 12:58:13 pm
Yep, I'm with bplus.  How did this get in here? And with replies - not allowable in Library.  What is going on?

It's a conspiracy, though that would be mere Conjecture.

Later: well, it's not "here (Library)" any more.  We ought, I think, to look into how it could have got there in the first place.  Sorry mathman1, to be writing all over your post.

Title: Re: Collatz Conjecture
Post by: bplus on July 07, 2020, 12:59:10 pm
Thanks Odin! 👍

@Mathman1@comcast.net

The Secret life of Numbers, I've heard of that. Who was author and year of Copy Right?

Not Ian Stewart by any chance? Or Clifford Pickover?

Update: Keep forgetting this is Internet Age and I can just look it up, so 2006 collection of math articles by George Szpiro.
Title: Re: Collatz Conjecture
Post by: Mathman1@comcast.net on July 07, 2020, 01:47:11 pm
The book is The Secret Life of Numbers : 50 Easy Pieces on How Mathematicians Work and Think by George G. Szpiro, Copyright 2006. George Szpiro had a previous book Kempler's Conjecture as mention on the back cover.
Title: Re: Collatz Conjecture
Post by: bplus on July 07, 2020, 02:48:02 pm
I wrote a program that will write a program like this:
Code: QB64: [Select]
  1. PRINT "Testing number  9 " + CHR$(10): GOTO 9 '<<<<< 2-times plug-in number <= 10
  2.  
  3. 1 PRINT " 1! Collatz Conjecture Confirmed.": END
  4. 2 PRINT " 2 EVEN ": GOTO 1
  5. 3 PRINT " 3 ODD ": GOTO 10
  6. 4 PRINT " 4 EVEN ": GOTO 2
  7. 5 PRINT " 5 ODD ": GOTO 16
  8. 6 PRINT " 6 EVEN ": GOTO 3
  9. 7 PRINT " 7 ODD ": GOTO 22
  10. 8 PRINT " 8 EVEN ": GOTO 4
  11. 9 PRINT " 9 ODD ": GOTO 28
  12. 10 PRINT " 10 EVEN ": GOTO 5
  13. 11 PRINT " 11 ODD ": GOTO 34
  14. 13 PRINT " 13 ODD ": GOTO 40
  15. 14 PRINT " 14 EVEN ": GOTO 7
  16. 16 PRINT " 16 EVEN ": GOTO 8
  17. 17 PRINT " 17 ODD ": GOTO 52
  18. 20 PRINT " 20 EVEN ": GOTO 10
  19. 22 PRINT " 22 EVEN ": GOTO 11
  20. 26 PRINT " 26 EVEN ": GOTO 13
  21. 28 PRINT " 28 EVEN ": GOTO 14
  22. 34 PRINT " 34 EVEN ": GOTO 17
  23. 40 PRINT " 40 EVEN ": GOTO 20
  24. 52 PRINT " 52 EVEN ": GOTO 26
  25.  

Why?

(something about being a nerd)

Reminder if it's odd then  multply by 3 and add 1, 9 literally goes to 28

if it's even like 28 then it goes to half, 14 in the case of 28.

So on and so on and it always ends up at 1 according to Collatz Conjecture. Prove it and you can make some money I think.


Quote
I do not know how to use the MOD function for the odd/even test so I divide and multiply by 2 and test for equivalency with the original number.
@Mathman1@comcast.net

If N MOD 2 = 1 then N is Odd  else it's Even.

If N MOD M = 0 then N is divisible M.

Think of MOD as remainder If the remainder of N/M is 0 then N is divisible by M. MOD Comes in very handy for programming.
Title: Re: Collatz Conjecture
Post by: _vince on July 07, 2020, 09:22:48 pm
I sometimes like to fantasize that there's an alien mathematics out there, completely unlike ours, where this problem is trivial
Title: Re: Collatz Conjecture
Post by: Qwerkey on July 08, 2020, 03:34:18 am
I sometimes like to fantasize ...

"Start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1."

Well it shows the type of people we are: fantasising over that!!!
Title: Re: Collatz Conjecture
Post by: _vince on July 08, 2020, 11:19:07 am
"Start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1."

Completely self evident trivial case given the nature of difluranic branching operations on the field of dark integers
Title: Re: Collatz Conjecture
Post by: bplus on July 08, 2020, 12:43:38 pm
OK last night and this morning I been working on a rule change to Collatz:

When the number is odd do this: (3*N + 1)/2 because with the bare 3*N + 1 your next step IS ALWAYS to divide by 2 because an Odd + 1 ALWAYS makes an Even number, automatically dividing by 2 saves a step and more importantly to computer programs keeps the highest working number (it's working with) considerably lower! (so you don't hit the Type limit so soon).

Because this is how you could work an equivalent expression:

(((N + 1) / 2) * 3) - 1 = (3*N + 1) / 2 and the computer math on the left expression never exceeds ~~ 1.5 * N (+/-) as opposed to 3 * N + 1


If the number is odd then add 1 (makes the odd number even) so you can divide by 2 (and stay an Integer) then multiply by 3 (this gives us a higher limit we can go with our number Type, nearly doubling it!) now you subtract 1 because when adding 1 before dividing by 2 you are really adding 2.

Before limit for Integers is 32XXX/ 3, with rule change that gets equivalent results limit for Integers is (32XXX / 3) * 2. So you've effectively doubled the numbers you can work with a given Type.

Title: Re: Collatz Conjecture
Post by: bplus on July 09, 2020, 02:30:17 am
Code: QB64: [Select]
  1. ' 2020-07-09 Write up  from reply I left at QB64 forum yesterday
  2. ' ref: https://www.qb64.org/forum/index.php?topic=2789.msg120521#msg120521
  3. ' here is essence  (((N + 1) / 2) * 3) - 1 = (3*N + 1) / 2   is New Rule for Odd
  4. ' here is new limit (32766 / 3) * 2
  5.  
  6. _TITLE "Collatz Array Program revist 2020-07-09"
  7. CONST topLimit = (32766 / 3) * 2 'just integers today
  8. REDIM cSeq$(0)
  9.     PRINT "The highest number Interger Type can handle is"; topLimit; ","
  10.     PRINT "If our test number wanders up there we must cut the run short."
  11.     PRINT "Go ahead try 20,000 it is over 1/3 of Integer Type Limit!"
  12.     INPUT "(0 quits) Enter a number to try New Collatz Sequencer "; n
  13.     IF n = 0 THEN EXIT DO
  14.     done = 0: odd = 0: even = 0: l = 1
  15.     WHILE n <> 1 OR done
  16.         s$ = STR$(l) + ":" + STR$(n)
  17.         IF n MOD 2 THEN 'odd
  18.             IF n < topLimit THEN s$ = s$ + " odd": l = l + 1 ELSE s$ = s$ + " hit limit, end of run.": done = 1
  19.             n = (((n + 1) / 2) * 3) - 1: odd = odd + 1
  20.         ELSE
  21.             s$ = s$ + " even": l = l + 1
  22.             n = n \ 2: even = even + 1
  23.         END IF
  24.         sAppend cSeq$(), s$
  25.     WEND
  26.     IF done = 0 THEN
  27.         s$ = STR$(l) + ":" + STR$(n)
  28.         sAppend cSeq$(), s$
  29.         even = even + 1
  30.     END IF
  31.     s$ = STR$(odd) + " times the odd rule was applied.": sAppend cSeq$(), s$
  32.     s$ = STR$(even) + " times the even  rule was applied.": sAppend cSeq$(), s$
  33.     s$ = STR$(odd + even) + " steps were taken to create this sequence.": sAppend cSeq$(), s$
  34.     display cSeq$()
  35.     REDIM cSeq$(0)
  36.     CLS
  37. PRINT: PRINT "0 tells me to say, goodbye!"
  38.  
  39. SUB sAppend (arr() AS STRING, addItem$)
  40.     REDIM _PRESERVE arr(LBOUND(arr) TO UBOUND(arr) + 1) AS STRING
  41.     arr(UBOUND(arr)) = addItem$
  42.  
  43. SUB display (arr() AS STRING)
  44.     lb = LBOUND(arr): ub = UBOUND(arr)
  45.     IF ub - lb + 1 < 21 THEN top = ub ELSE top = lb + 19
  46.     CLS: PRINT "press any key to quit scroller..."
  47.     LOCATE 3, 1
  48.     FOR i = lb TO top
  49.         PRINT arr(i)
  50.     NEXT
  51.     DO
  52.         IF ub - lb + 1 > 20 THEN
  53.             DO WHILE _MOUSEINPUT
  54.                 IF row >= lb THEN row = row + _MOUSEWHEEL ELSE row = lb 'prevent under scrolling
  55.                 IF row > ub - 19 THEN row = ub - 19 'prevent over scrolling
  56.                 IF prevrow <> row THEN 'look for a change in row value
  57.                     IF row >= lb AND row <= ub - 19 THEN
  58.                         CLS: PRINT "press any key to quit scroller..."
  59.                         LOCATE 3, 1
  60.                         FOR n = row TO row + 19
  61.                             PRINT arr(n)
  62.                         NEXT
  63.                     END IF
  64.                 END IF
  65.                 prevrow = row 'store previous row value
  66.             LOOP
  67.         END IF
  68.     LOOP UNTIL INKEY$ > ""
  69.  
  70.  
  71.  

So what? I just made the Odd Rule Odder! ;-))
Title: Re: Collatz Conjecture
Post by: bplus on July 09, 2020, 02:40:13 pm
Collatz Collapse, reducing Collatz Sequence to it's essential sequence of odd integers by removing all the Even Step fluff:

Code: QB64: [Select]
  1. ' 2020-07-09 Write up  from reply I left at QB64 forum yesterday
  2. ' ref: https://www.qb64.org/forum/index.php?topic=2789.msg120521#msg120521
  3. ' here is essence  (((N + 1) / 2) * 3) - 1 = (3*N + 1) / 2   is New Rule for Odd
  4. ' here is new limit (32766 / 3) * 2
  5.  
  6. ' 2020-07-09 A dream I had this morning to change 2 rules into 1 (almost) in order to simplify
  7. ' the Collatz sequence into the significant sequence of ascending odd numbers.
  8. ' What do they look like in terms of prime numbers?
  9.  
  10.  
  11. _TITLE "Collatz Collapse 2020-07-09"
  12. CONST topLimit = (32766 / 3) * 2 'just integers today
  13. REDIM cSeq$(0)
  14.    1 PRINT "The highest number Interger Type can handle is"; topLimit; ","
  15.     PRINT "If our test number wanders up there we must cut the run short."
  16.     PRINT "Go ahead try 20,000 it is over 1/3 of Integer Type Limit!"
  17.     INPUT "(0 quits) Enter a number to try New Collatz Sequencer "; n
  18.     IF n = 0 THEN EXIT DO
  19.     IF n > topLimit THEN GOTO 1
  20.     done = 0: odd = 0: even = 0: l = 1
  21.     sAppend cSeq$(), "Starting number is" + STR$(n)
  22.     WHILE n MOD 2 = 0 'drop down and give me an odd number of pushups
  23.         n = n \ 2
  24.     WEND
  25.     WHILE n <> 1 OR done
  26.         s$ = STR$(l) + ":" + STR$(n)
  27.         IF n MOD 2 THEN 'odd
  28.             IF n < topLimit THEN
  29.                 s$ = s$ + " odd": l = l + 1
  30.                 n = (((n + 1) / 2) * 3) - 1
  31.                 odd = odd + 1
  32.             ELSE
  33.                 s$ = s$ + " hit limit, end of run.": done = 1
  34.             END IF
  35.         ELSE
  36.             s$ = s$ + " even": l = l + 1
  37.             n = n \ 2: even = even + 1
  38.         END IF
  39.         WHILE n MOD 2 = 0 'drop down and give me an odd number of pushups
  40.             n = n \ 2
  41.         WEND
  42.         sAppend cSeq$(), s$
  43.     WEND
  44.     IF done = 0 THEN
  45.         s$ = STR$(l) + ":" + STR$(n)
  46.         sAppend cSeq$(), s$
  47.         even = even + 1
  48.     END IF
  49.     sAppend cSeq$(), "------------------------------------------------------------------"
  50.     s$ = STR$(odd) + " odd steps were taken to create this sequence.": sAppend cSeq$(), s$
  51.     display cSeq$()
  52.     REDIM cSeq$(0)
  53.     CLS
  54. PRINT: PRINT "0 tells me to say, goodbye!"
  55.  
  56. SUB sAppend (arr() AS STRING, addItem$)
  57.     REDIM _PRESERVE arr(LBOUND(arr) TO UBOUND(arr) + 1) AS STRING
  58.     arr(UBOUND(arr)) = addItem$
  59.  
  60. SUB display (arr() AS STRING)
  61.     lb = LBOUND(arr): ub = UBOUND(arr)
  62.     IF ub - lb + 1 < 21 THEN top = ub ELSE top = lb + 19
  63.     CLS: PRINT "press any key to quit scroller..."
  64.     LOCATE 3, 1
  65.     FOR i = lb TO top
  66.         PRINT arr(i)
  67.     NEXT
  68.     DO
  69.         IF ub - lb + 1 > 20 THEN
  70.             DO WHILE _MOUSEINPUT
  71.                 IF row >= lb THEN row = row + _MOUSEWHEEL ELSE row = lb 'prevent under scrolling
  72.                 IF row > ub - 19 THEN row = ub - 19 'prevent over scrolling
  73.                 IF prevrow <> row THEN 'look for a change in row value
  74.                     IF row >= lb AND row <= ub - 19 THEN
  75.                         CLS: PRINT "press any key to quit scroller..."
  76.                         LOCATE 3, 1
  77.                         FOR n = row TO row + 19
  78.                             PRINT arr(n)
  79.                         NEXT
  80.                     END IF
  81.                 END IF
  82.                 prevrow = row 'store previous row value
  83.             LOOP
  84.         END IF
  85.     LOOP UNTIL INKEY$ > ""
  86.  
  87.  
Title: Re: Collatz Conjecture
Post by: bplus on July 10, 2020, 11:59:41 am
Quote
@bplus: Why you don't use the type _UNSIGNED _INTEGER, your upper limit is much higher,
as I have used it in my old program?

A reasonable question from those impressed by big numbers.

If I can't discover the essence of what makes Collatz tick in the Integer range then I am not so interested in the problem. I do know I have the option to go the way you suggest, if I have to, as yet I am not so compelled. With number there is no end to going bigger, it's an essential part of the proof process if something happens for small number it will also happen with big. "As above, so below." or is it the other way around? Well if it's universal, it is the same above and below.