Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Performing rules depending on whether input value is even or odd until the value equals 1

Subject: Performing rules depending on whether input value is even or odd until the value equals 1

From: Ar

Date: 4 Sep, 2013 02:57:09

Message: 1 of 3

How should I go about solving this.

If n is even, divide it by 2 to get n/2.
If n is odd, multiply it by 3 and add 1 to get 3n + 1.

I have to write a function that will take any natural number as function input, apply
the above rules and outputs the number iterations taken for that number to reduce to 1.

Where the function starts as follows:
function [iterations] = OddEven(x)

Subject: Performing rules depending on whether input value is even or odd until the value equals 1

From: Roger Stafford

Date: 4 Sep, 2013 04:12:19

Message: 2 of 3

"Ar " <ariq.quazifc@gmail.com> wrote in message <l067i5$qhn$1@newscl01ah.mathworks.com>...
> If n is even, divide it by 2 to get n/2.
> If n is odd, multiply it by 3 and add 1 to get 3n + 1.
> I have to write a function that will take any natural number as function input, apply
> the above rules and outputs the number iterations taken for that number to reduce to 1.
- - - - - - - -
  That comes from the well-known Collatz conjecture. Lothar Collatz conjectured that no matter what positive integer is chosen, the following sequence always arrives at 1 if it is followed sufficiently far.

  You need to show some of what you have tried to get help at this site.

Roger Stafford

Subject: Performing rules depending on whether input value is even or

From: Nasser M. Abbasi

Date: 4 Sep, 2013 04:19:47

Message: 3 of 3

On 9/3/2013 9:57 PM, Ar wrote:
> How should I go about solving this.
>
> If n is even, divide it by 2 to get n/2.
> If n is odd, multiply it by 3 and add 1 to get 3n + 1.
>
> I have to write a function that will take any natural number as function input, apply
> the above rules and outputs the number iterations taken for that number to reduce to 1.
>
> Where the function starts as follows:
> function [iterations] = OddEven(x)
>

Just make sure to enter your name and date etc... below, I left
that blank

--------------------------------------
function k = OddEven(n)
%student name: <enter name>
%date: <enter date>

if round(n) ~= n || n<=0
     if n<=0
         error('input must be positive');
     else
         error('input must be integer');
     end
end

k = 0;
while n ~= 1
     if mod(n,2)
         n = n*3 + 1;
     else
         n = n/2;
     end
     k = k + 1;
end

end
----------------------

EDU>> k=OddEven(8)
      3

EDU>> k=OddEven(19)
     20

EDU>> k=OddEven(7)
     16


--Nasser

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us