This is due tomorrow, and i am absoltuly lost, can anyone help with some examples or point in the right direction, MANY THANKS for any help
Mathematician Stanislav Ulam proposed that any positive integer would reduce to 1 if the following algorithm was repeated a sufficient number of times.

If the number is even, divide it by 2.
If the number is odd, multiply it by 3 and then add 1.

For example, if the original number were 13, the algorithm would generate the following sequence of numbers: 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. This sequence has 10 terms counting the original number and the 1.

Write a program that accomplishes the following three functions:

Prompts the user for a positive integer input, or a zero to exit. This prompt should be repeated if the user enters a negative number.
Outputs all the numbers in the sequence produced by the Ulam algorithm.
Counts how many numbers are in the sequence, and outputs the count.

Dani AI

Generated

A few practical notes and a short, safe Pascal example to complement the replies already here. 's recursion idea is fine conceptually, but recursion risks stack overflow for long Collatz (Ulam) chains; an iterative loop is more robust. correctly flagged the parity test mistake (use n mod 2 = 0 for even); 's early attempt also accidentally performed an extra division in the odd branch — avoid that double-step.

procedure ShowCollatz(n: Int64);
var
  count: LongInt;
begin
  count := 0;
  while n > 0 do
  begin
    WriteLn(n); Inc(count);
    if n = 1 then Break;
    if Odd(n) then
      n := 3*n + 1
    else
      n := n div 2;
    if count > 10000000 then
    begin
      WriteLn('Iteration limit reached — stopping to avoid runaway loop.');
      Break;
    end;
  end;
  WriteLn('Terms: ', count);
end;

Key practical tips: always initialize the counter before each run and print the original value (count should include the starting number and the terminating 1). Validate input with a small loop that re-prompts on negatives and treats 0 as the exit sentinel. Prefer Int64 (or the largest integer your compiler supports) because 3*n+1 grows quickly; before 3*n+1 check for potential overflow (for example, test n <= (High(Int64)-1) div 3 when the compiler supports High).

These corrections address the common bugs seen in the thread (flipped parity, double-division, uninitialized counters, and recursion/overflow risk) while keeping the implementation straightforward and safe for classroom use.

Recommended Answers

All 8 Replies

>This is due tomorrow
Bummer.

>and i am absoltuly lost
Try paying attention in class.

>can anyone help with some examples or point in the right direction
Sure, post your attempt and we'll help. Nobody is going to do your homework for you. Especially since it seems like you're terribly lazy. I wrote this program in about 30 seconds, including the time it took to create, write and save the source file and the time to compile it.

Ahh the good 'ol days!

So the most fancy way to do this would be to use recursion.

function ulam(i: integer): integer;
var
 x: integer;
begin
 if i = 1 
  then 
   return i
  else
   Begin
    if (i mod 2) > 0 //even
     then
      x:=ulam(i div 2)
     else
      x:=ulam(i * 3 +1);
    end;
//do something with x
end;

not tested but i hope you get the picture! The prompt is ethe easy part and I will leave that up to you as well as the displaying of output. Good luck!

This is due tomorrow, and i am absoltuly lost, can anyone help with some examples or point in the right direction, MANY THANKS for any help
Mathematician Stanislav Ulam proposed that any positive integer would reduce to 1 if the following algorithm was repeated a sufficient number of times.

If the number is even, divide it by 2.
If the number is odd, multiply it by 3 and then add 1.

For example, if the original number were 13, the algorithm would generate the following sequence of numbers: 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. This sequence has 10 terms counting the original number and the 1.

Write a program that accomplishes the following three functions:

Prompts the user for a positive integer input, or a zero to exit. This prompt should be repeated if the user enters a negative number.
Outputs all the numbers in the sequence produced by the Ulam algorithm.
Counts how many numbers are in the sequence, and outputs the count.

This is due tomorrow
Bummer.

and i am absoltuly lost
Try paying attention in class.

can anyone help with some examples or point in the right direction
Sure, post your attempt and we'll help. Nobody is going to do your homework for you. Especially since it seems like you're terribly lazy. I wrote this program in about 30 seconds, including the time it took to create, write and save the source file and the time to compile it.

WOULD THIS HELP YOU INSTEAD OF ACCUSATIONS OF LAZYNESS??

PROGRAM week5Program1 (input, output);
       {$APPTYPE CONSOLE}
Uses
    sysutils;

var
   num1,count :integer;

begin
      writeln ('Enter a positive integer to see its Ulam sequence, or a zero to exit:');
      readln (num1);

      if (num1 = 0) then
         begin
         writeln ('Thank you! You have chosen to exit');
           writeln ('Press <enter> to exit.');
             readln;
       end;

      while (num1 < 1) and (num1 <> 0) do
        begin
          writeln (' The number you enter must be a postive number');
            writeln ('Enter a POSITIVE integer:');
              readln (num1);
      end;



     while (num1 <> 1) do

      BEGIN
        begin
         IF (num1 mod 2 <> 0) then           {if even}
           BEGIN
             num1 := num1 div 2 ;
               writeln (num1);
               count := count + 1
           END
         ELSE                                  {if odd}
             begin
             num1 := 3 * num1 + 1;
               num1 := num1 div 2;
                 writeln (num1)     ;
                   count := count + 1
             end
        end;

       writeln ('The sequence in the Ulam algorithm for the number you entered are:',num1);
         writeln ('There are',count,'numbers in the sequence.');
           writeln ('Press <enter> to continue.');
             readln;
      END;    

end.

egin
writeln ('Please enter a positive integer:');
readln (num1);

WHILE (num1 < 0) do
writeln ('THE INTEGER MUST BE POSITIVE (>0) TO CONTINUE.');

repeat
begin
begin
IF num1 = (num1 mod 2 <> 0) then
begin
num1 := num1 div 2 ;
count := count +1 ;
end
ELSE
num1 := 3*num1 +1 ;

until (num1 :=1)
end;

writeln ('Ulam sequence is: ',num1);
writeln ('Count is :',count);
writeln ('Press <enter>.');
readln ;

end.

I ALSO TRIED IT LIKE THIS. THANKS AGAIN FOR ANY HELP

>WOULD THIS HELP YOU INSTEAD OF ACCUSATIONS OF LAZYNESS??
Yes, as a matter of fact it does. By the way, do you realize that num1 is even if "num1 mod 2 = 0" and odd otherwise? You have your conditionals flip-flopped.

Thanks, i just found that and got it to work with a couple of changes, i do appreciate your help. And i will probably be back for more.
C

>i do appreciate your help
I'm glad things worked out for you. :)

Can anyone give any ideas on a program... It has to be unique... It's our final, and we're supposed to come up with some program... i'm not sure what to do... any help would be greatly appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.