943,507 Members | Top Members by Rank

Ad:
Oct 1st, 2004
0

using pascal to write ulam sequence

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PascalRookie is offline Offline
7 posts
since Oct 2004
Oct 2nd, 2004
0

Re: using pascal to write ulam sequence

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 2nd, 2004
0

Re: using pascal to write ulam sequence

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!

Quote 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.
Reputation Points: 13
Solved Threads: 5
Light Poster
Venjense is offline Offline
31 posts
since Oct 2003
Oct 3rd, 2004
0

Re: using pascal to write ulam sequence

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PascalRookie is offline Offline
7 posts
since Oct 2004
Oct 3rd, 2004
0

Re: using pascal to write ulam sequence

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PascalRookie is offline Offline
7 posts
since Oct 2004
Oct 3rd, 2004
0

Re: using pascal to write ulam sequence

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2004
0

Re: using pascal to write ulam sequence

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PascalRookie is offline Offline
7 posts
since Oct 2004
Oct 3rd, 2004
0

Re: using pascal to write ulam sequence

>i do appreciate your help
I'm glad things worked out for you.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 30th, 2004
0

Re: using pascal to write ulam sequence

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
Reputation Points: 10
Solved Threads: 0
Light Poster
ediehm is offline Offline
27 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: TextOut to Printer
Next Thread in Pascal and Delphi Forum Timeline: Help I'm sleepy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC