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.

## 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.

``````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:');

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

while (num1 < 1) and (num1 <> 0) do
begin
writeln (' The number you enter must be a postive number');
writeln ('Enter a POSITIVE integer:');
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.');
END;

end.
``````

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

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>.');

end.

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