using pascal to write ulam sequence

Reply

Join Date: Oct 2004
Posts: 7
Reputation: PascalRookie is an unknown quantity at this point 
Solved Threads: 0
PascalRookie PascalRookie is offline Offline
Newbie Poster

using pascal to write ulam sequence

 
0
  #1
Oct 1st, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,308
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: using pascal to write ulam sequence

 
0
  #2
Oct 2nd, 2004
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 18
Reputation: Venjense is an unknown quantity at this point 
Solved Threads: 1
Venjense's Avatar
Venjense Venjense is offline Offline
Newbie Poster

Re: using pascal to write ulam sequence

 
0
  #3
Oct 2nd, 2004
Ahh the good 'ol days!

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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. function ulam(i: integer): integer;
  2. var
  3. x: integer;
  4. begin
  5. if i = 1
  6. then
  7. return i
  8. else
  9. Begin
  10. if (i mod 2) > 0 //even
  11. then
  12. x:=ulam(i div 2)
  13. else
  14. x:=ulam(i * 3 +1);
  15. end;
  16. //do something with x
  17. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: PascalRookie is an unknown quantity at this point 
Solved Threads: 0
PascalRookie PascalRookie is offline Offline
Newbie Poster

Re: using pascal to write ulam sequence

 
0
  #4
Oct 3rd, 2004
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: PascalRookie is an unknown quantity at this point 
Solved Threads: 0
PascalRookie PascalRookie is offline Offline
Newbie Poster

Re: using pascal to write ulam sequence

 
0
  #5
Oct 3rd, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,308
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: using pascal to write ulam sequence

 
0
  #6
Oct 3rd, 2004
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: PascalRookie is an unknown quantity at this point 
Solved Threads: 0
PascalRookie PascalRookie is offline Offline
Newbie Poster

Re: using pascal to write ulam sequence

 
0
  #7
Oct 3rd, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,308
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: using pascal to write ulam sequence

 
0
  #8
Oct 3rd, 2004
>i do appreciate your help
I'm glad things worked out for you.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 27
Reputation: ediehm is an unknown quantity at this point 
Solved Threads: 0
ediehm ediehm is offline Offline
Light Poster

Re: using pascal to write ulam sequence

 
0
  #9
Dec 30th, 2004
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum


Views: 5433 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC