Recursion - HalfLife Calculation

I am working on a recursion function that determines the amount of radioactive material that remains after a given number of years. The decay rate is 1/2 or 50% each year.

I am having a trouble trying to get the recursion setup.

i just cannot seem to get the recursive case right implementing the formula.

The correct result that should finally be returned is 31.25.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int halflife(int, int);

    int main()
    {
     cout << "The amount of 500 lbs of material after 5 years is " 
     		<< halflife(500, 5) << endl;
    }


    int halflife(int initial, int year)
    {
      if (year == 0)
      return 1;
      else
      return  (halflife(initial, year - 1) * .5);
    }

right now the issue is it is returning 0.03125. which as you can see i very close if i just multiply the result by 1000 however i cannot do that. Any help will be nice
Last edited on
Please clarify. Are you saying the radioactive level measures 500 at the beginning and the level will decrease by 50% each time interval and you have 5 time intervals?

If yes then your decay should look like:

5=500 then 4=250 then 3=125 then 2=62.5 then 1=31.25 then 0=15.6

So I am not sure where you get the value 0.488281
The radioactive level measures 500 at the beginning and decrease by 50% or 1/2 each interval.

So the Radioactive Material starting is 500
the decay is 50% or 1/2
the total amount of time of decay is 5 years

What should return is 31.25

Your interval above is going 6 times and not 5.
Last edited on
edited
What do you mean by "HalfLife"?
Is it the famous "Half Life" game you are talking about?
Last edited on
int halflife(int initial, int year)
Integer division truncates the fractional part.


What should return is 31.25

Your interval above is going 6 times and not 5.

https://puu.sh/rJgqt/d7aef44b70.png

1
2
if (year == 0)
      return 1;

How do you know that after n years, there will be exactly 1lb left?

1
2
3
4
5
6
7
8
9
double halflife( int initial, int year )
{
    if( year == 0 ) return 1;
    else {
        double amount{ halflife( initial, year - 1 ) * .5 };
        std::cout << "year = " << year << "\tamount = " << amount << "\n";
        return amount;
    }
}


year = 1	amount = 0.5
year = 2	amount = 0.25
year = 3	amount = 0.125
year = 4	amount = 0.0625
year = 5	amount = 0.03125
The amount of 500 lbs of material after 5 years is 0.03125



right now the issue is it is returning 0.03125. which as you can see i very close if i just multiply the result by 1000 however i cannot do that.

That is just a coincidence.
1
2
3
4
5
6
int main( )
{
    const int initial{ 600 }, time{ 5 };
    
     std::cout << "The amount of " << initial << " lbs of material after " << time << " years is " << halflife( initial, time ) << "\n";
}


year = 1	amount = 0.5
year = 2	amount = 0.25
year = 3	amount = 0.125
year = 4	amount = 0.0625
year = 5	amount = 0.03125
The amount of 600 lbs of material after 5 years is 0.03125
Last edited on
So now I am completely lost if the calculation is just a coincidence.

Basically I need it to take the recursive function to take a value and cut it in half each time it iterates. using the Year as the counter.

I know that the correct answer if the initial value = 1000 decaying 50% or 1/2 per year for 5 years is 31.25.

just cannot seem to get the function to work as it should...any help please would be appreciated
You can either do it like this:
1
2
3
4
5
6
7
8
9
double halflife( int initial, int year )
{
    if( year == 0 ) return initial;
    else {
        double amount{ halflife( initial, year - 1 ) * .5 };
        std::cout << "year = " << year << "\tamount = " << amount << "\n";
        return amount;
    }
}


year = 1	amount = 250
year = 2	amount = 125
year = 3	amount = 62.5
year = 4	amount = 31.25
year = 5	amount = 15.625
The amount of 500 lbs of material after 5 years is 15.625


or

1
2
3
4
5
6
7
double halflife( double initial, int year )
{
    std::cout << "year = " << year << "\tamount = " << initial << "\n";
    
    if( year == 0 ) return initial;
    else return halflife( initial * 0.5, year - 1 );
}


year = 5	amount = 500
year = 4	amount = 250
year = 3	amount = 125
year = 2	amount = 62.5
year = 1	amount = 31.25
year = 0	amount = 15.625
The amount of 500 lbs of material after 5 years is 15.625


With recursive functions, it's extremely helpful to output the values of the parameters, so you can see how the computer unfolds the recursion.
Last edited on
Thanks, I was able to see that my multiplication of the 1/2 was in the wrong spot and I was returning the wrong value in my base case which is why my return was not coming up incorrectly.
Last edited on
Topic archived. No new replies allowed.