Ulam's Algorithm

For loop to divide even by 2 & mutiply odd by 3 & add 1.

Page 1 of 1

7 Replies - 7509 Views - Last Post: 01 December 2008 - 01:26 AM Rate Topic: -----

#1 cball1321  Icon User is offline

  • New D.I.C Head

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

Ulam's Algorithm

Post icon  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 ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Ulam's Algorithm

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 cball1321  Icon User is offline

  • New D.I.C Head

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

Re: Ulam's Algorithm

Posted 30 November 2008 - 10:55 PM

View PostKYA, 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;
}






Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 cball1321  Icon User is offline

  • New D.I.C Head

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

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:10 PM

View PostKYA, 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.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • 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.


edited to add additonal comments in the code

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

Was This Post Helpful? 0
  • +
  • -

#7 cball1321  Icon User is offline

  • New D.I.C Head

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

Re: Ulam's Algorithm

Posted 30 November 2008 - 11:46 PM

View PostKYA, 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.


edited to add additonal comments in the code

Has to be greater thsn 1 and cannot use break .
Was This Post Helpful? 0
  • +
  • -

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • 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);     
}  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1