| | |
using pascal to write ulam sequence
![]() |
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
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.
In case you were wondering, yes, I do hate you.
Ahh the good 'ol days!
So the most fancy way to do this would be to use recursion.
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!
So the most fancy way to do this would be to use recursion.
Pascal and Delphi Syntax (Toggle Plain Text)
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!
•
•
•
•
Originally Posted by PascalRookie
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.
Venjense
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:');
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.
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- pascal write problem (ASP.NET)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: TextOut to Printer
- Next Thread: Help I'm sleepy
Views: 5433 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi
animation api app array button compile console data database dbisam delete delphi delphihelpimageforloop documentcomplete2 edit environment error errors events file form function gdi gis lasrautoinc media navigatecomplete2 network object open opengl pascal passing path player procedure search set sql table twebbrowser username variable win7 windows






