Can such a program be written?
Yes
 100%  [ 9 ]
No
 0%  [ 0 ]
Total Votes : 9

Collatz Conjecture

[Pick a number ~ if it is odd, then [*3, +1]. If it is even, then [Divide by 2]. Keep doing this. You will eventually reach a 4-2-1 loop.]

Picture a chart with 0-100 on the bottom & 0-100 on the left side. The bottom row will be for the starting number. The left side will be for the highest number reached before dropping to the 4-2-1 Loop for that starting number. Can such a program be written?

Someone asked what language I wanted. I am not sure of the language, but I wuld like to program it into my TI-~83 Plus.
What makes you think such a program couldn't be written? I don't see anything crazy going on here.
Edit: I made the program both for the number of iterations and for the max value reached.



Code:
#include <stdlib.h>
#include <graphx.h>
#include <tice.h>


int main(void)
{
    int num, max, iterations = 0;
    char title1[] = "Collatz Iterations";
    char title2[] = "Collatz Max Value";
    gfx_Begin();
    //setup
    gfx_SetDrawScreen();
    gfx_ZeroScreen();
    gfx_SetColor(255);
    gfx_SetTextFGColor(255);
    gfx_SetTextBGColor(5);
    gfx_SetTextTransparentColor(5);
    gfx_SetTextScale(2,2);
    gfx_PrintStringXY(title1,(LCD_WIDTH - gfx_GetStringWidth(title1)) / 2, 3);
    for (int i=1;i<LCD_WIDTH - 1;i++){
        num = i;
        iterations = 1;
        do{
            if(num % 2 == 0){
                num = num / 2;
            } else {
                num = num * 3 + 1;
            }
            iterations++;
        } while (num != 4);
        gfx_FillRectangle_NoClip(i, LCD_HEIGHT - iterations, 2, 2);
    }
    while (!os_GetCSC());
    gfx_ZeroScreen();
    gfx_PrintStringXY(title2,(LCD_WIDTH - gfx_GetStringWidth(title2)) / 2, 3);
    for (int i=1;i<LCD_WIDTH - 1;i++){
        num = i;
        max = 1;
        do{
            if(num % 2 == 0){
                num = num / 2;
            } else {
                num = num * 3 + 1;
            }
            if (max < num){
                max = num;
            }
        } while (num != 4);
        gfx_FillRectangle_NoClip(i, LCD_HEIGHT - (max / 55) , 2, 2);
    }
    while (!os_GetCSC());
    gfx_End();
    return 0;
}


Download
Yes, that's a relatively easy program. Without actually making it show a table, you could throw it together like this:


Code:
Input "STARTING NUMBER:",X
Repeat getKey
   If remainder(X,2):Then
      3X+1->X
   Else
      .5X->X
   End
   Disp X
End


You can stop this version by pressing any key. You could also store the three most recent values in a list and stop when the last three are [4, 2, 1].

Code:
def callitz():
  def cg(n):
    yield n
    while n != 1:
      n = 3 * n + 1 if n % 2 else n // 2
      yield n

  for n in range(1, 101):
    print(n, max(list(cg(n)))

You didn't specify a language, but the bottom line is that it is definitely possible (as womp said, why wouldn't it be?).

Also, 0 as a starting point is invalid as posed, since 0 never reaches the 4-2-1 loop. Note additionally that many values <100 reach a rather large maximum.

Edit: thought I'd golf this in TI-BASIC, cause that's what we do here after all.
Display code could be adjusted as desired; current form is 50 49 bytes.

Code:
:For(I,1,|E2
:I->M
:While ln(Ans
:.5Ans
:If fPart(Ans:6Ans+1
:If Ans>M:Ans->M
:End
:Disp toString(I
:Pause M
:End
For what it's worth, :While Ans!=1 -> :While Ans-1.
KermMartian wrote:
For what it's worth, :While Ans!=1 -> :While Ans-1.

That doesn't offer any size benefits, correct? Both ≠ and - are single byte tokens, so is there any reason to use - besides being easier to type?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement