Ulam's Algorithm

Page 1 of 1

7 Replies - 9648 Views - Last Post: 01 December 2008 - 01:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=74329&amp;s=d26646d526dcd22aaf4de385115ac2e9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

Ulam's Algorithm

Posted 30 November 2008 - 10:33 PM

```int next_term (int current_term)
{
for (current_term; current_term >=1; current_term--)
{
if ( current_term % 2 == 0)
current_term = current_term / 2;
if ( current_term % 2 > 0)
current_term = (current_term * 3) + 1;

cout << current_term << "	";
}

return (current_term);

}

```

** Edit **

Is This A Good Question/Topic? 0

Replies To: Ulam's Algorithm

#2 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

Re: Ulam's Algorithm

Posted 30 November 2008 - 10:35 PM

Any problems or unexpected output? Your code looks to handle the directions well.

#3 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

Re: Ulam's Algorithm

Posted 30 November 2008 - 10:55 PM

KYA, on 30 Nov, 2008 - 09:35 PM, said:

Any problems or unexpected output? Your code looks to handle the directions well.

If I enter an od number the first number displayed is correct then I have no idea what the program does after that.
I'll attached the entire code and let me know what you see and how to correct it.
```# include <iostream>
using namespace std;

int next_term (int current_term)
{
for (current_term; current_term >=1; current_term--)
{
if ( current_term % 2 == 0)
current_term = current_term / 2;
if ( current_term % 2 > 0)
current_term = (current_term * 3) + 1;

cout << current_term << "	";
}

return (current_term);

}

int main ()
{
int current_term;
int series;

cout << "This program will display sequence of numbers. " << endl << endl;

cout << "Enter the first number in the sequence. ";
cin >> current_term;
cout << endl;

while ( current_term <= 1 )
{
cout << "Error !! Re-enter a number greater than 1. ";
cin >> current_term;
cout << endl;
}

series = next_term (current_term);

cout << endl << endl;
system ("pause");
return 0;
}

```

#4 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:02 PM

There's no need for an initialization statement in your for loop within the function:

```int next_term (int current_term)
{
for (; current_term >=1; current_term--)
{
if ( current_term % 2 == 0)
current_term = current_term / 2;
if ( current_term % 2 > 0)
current_term = (current_term * 3) + 1;

cout << current_term << "    ";
}

return (current_term);

}

```

The program spits out a bunch of numbers. I entered '3' for example, got 10, then 5, then 16...so on and so forth, but you have no base condition, so it goes on for a while, eventually wrapping around the maximum capacity of a signed integer.

More specifically your for loop is set to execute while the term is greater then or equal to 1. I'm thinking maybe a temp variable would be better for that otherwise the function goes on for longer then probably intended. Could you give a brief description of what exactly is supposed to be going on?

edited for typos

This post has been edited by KYA: 30 November 2008 - 11:02 PM

#5 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:10 PM

KYA, on 30 Nov, 2008 - 10:02 PM, said:

There's no need for an initialization statement in your for loop within the function:

```int next_term (int current_term)
{
for (; current_term >=1; current_term--)
{
if ( current_term % 2 == 0)
current_term = current_term / 2;
if ( current_term % 2 > 0)
current_term = (current_term * 3) + 1;

cout << current_term << "    ";
}

return (current_term);

}

```

The program spits out a bunch of numbers. I entered '3' for example, got 10, then 5, then 16...so on and so forth, but you have no base condition, so it goes on for a while, eventually wrapping around the maximum capacity of a signed integer.

More specifically your for loop is set to execute while the term is greater then or equal to 1. I'm thinking maybe a temp variable would be better for that otherwise the function goes on for longer then probably intended. Could you give a brief description of what exactly is supposed to be going on?

edited for typos

The program is supposed to divide the even numbers by two and multiply the odd numbers by 3 and add 1 until the number is down to 1. The program is supposed to do this for up to 15 numbers and keep track of how many numbers are used in the loop.

#6 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:17 PM

Can it be one? If so, solution:

```int next_term (int current_term)
{
int counter = 0;
while (current_term >= 1)//get rid of = if it cannot be '1'
{
if (counter == 15)//only do up to 15 iterations
{
break;
}
else
{
if ( current_term % 2 == 0)//even
{
current_term = current_term / 2;
}
else if ( current_term % 2 > 0)//odd
{
current_term = (current_term * 3) + 1;
}
cout << current_term << "    ";
counter++;
}

}
return (current_term);
}

```

If not remove the = sign and it will run just as well. Your original implementation and description do not match.

This post has been edited by KYA: 30 November 2008 - 11:17 PM

#7 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:46 PM

KYA, on 30 Nov, 2008 - 10:17 PM, said:

Can it be one? If so, solution:

```int next_term (int current_term)
{
int counter = 0;
while (current_term >= 1)//get rid of = if it cannot be '1'
{
if (counter == 15)//only do up to 15 iterations
{
break;
}
else
{
if ( current_term % 2 == 0)//even
{
current_term = current_term / 2;
}
else if ( current_term % 2 > 0)//odd
{
current_term = (current_term * 3) + 1;
}
cout << current_term << "    ";
counter++;
}

}
return (current_term);
}

```

If not remove the = sign and it will run just as well. Your original implementation and description do not match.

Has to be greater thsn 1 and cannot use break .

#8 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

Re: Ulam's Algorithm

Posted 01 December 2008 - 01:26 AM

If you can't use a break statement, then this will work.

```int next_term (int current_term)
{
int counter = 0;
while (current_term > 1 && counter < 15)//just put your count check in here
{
if ( current_term % 2 == 0)//even
{
current_term = current_term / 2;
}
else if ( current_term % 2 > 0)//odd
{
current_term = (current_term * 3) + 1;
}
cout << current_term << "    ";
counter++;
}
return (current_term);
}

```