r/dailyprogrammer • u/Elite6809 1 1 • Oct 15 '14
[10/15/2014] Challenge #184 [Intermediate] Radioactive Decay
(Intermediate): Radioactive Decay
Radioactive decay occurs when an unstable atomic nucleus tries to make itself become more stable. It does this by spitting bits of itself out - like taking bits off your car to make it lighter. While radioactive decay is an entirely random process, the probability of one type of nucleus decaying per second is well-defined.
There are two ways of describing this probability. The first is using a constant called λ (lambda). λ describes the probability of a specific type of atomic nucleus decaying per second. If λ=0, the probability is 0 - meaning the nucleus never decays, and is thus stable. If λ=0.5, every second there is a 50% chance the nucleus will decay. You get the point.
The second is using a value called t½ (the half-life). This describes how long it takes for exactly half of a sample to decay. For example, if the half-life of a nucleus is 10 seconds, and you start with a sample of 2000 nuclei:
- At the start, 2000 atoms remain - nothing has happened yet.
- After 10 seconds, half of the 2000 atoms will have decayed. 1000 remain.
- After 20 seconds, half of those 1000 will have decayed. 500 remain.
- After 30 seconds, half of those 500 will have decayed. 250 remain.
And so on, and so forth. This is all very simple - until you introduce the concept of a decay chain. This describes how a starting nucleus will decay into a 'daughter' nucleus, which in turn decays again into another type of nucleus - this happens all the way down the chain until the nucleus becomes stable.
Your challenge is: given a decay chain and λ values for each type of nucleus in the chain, calculate the percentage of each nucleus in the sample over time. This can be done by random sampling (the simpler way), or calculation of the exponential decays (for mathematicians). You can choose which method to do it by.
Trouble understanding the concept?
This challenge introduces a physics concept which may be new to you - don't worry if you have some issues understand what's going on. Imagine you have a bag of 400 marbles. These marbles can be different colours - at the start of the experiment the marbles are all red.
Let's say a red marble has a 10% chance of turning into a green one per second. A green marble has a 50% chance of turning into blue marble every second. This means that (roughly) 40 red marbles will have turned into green marbles after 1 second. We now have 360 red and 40 green.
Now, you have 360 red marbles and 40 green ones. Remember the green marbles have a 50% chance of turning into blue marbles. After another second (2 seconds in total), 50% of the green marbles turn into blue marbles, that is 20 of them. 10% of the remaining red marbles will turn green, that is 36 of them.
We now have 324 (400*90%*90%) red, 56 (the 20 that remain, and then the other 36 that just decayed) green and 20 blue. This carries on happening. Of course these are approximations, as we don't count any marbles that happen to change twice in one second. If the percentages of changing are low enough, however, this is negligible for our simulation.
Now, replace marble colour with 'type of nucleus' (aka. isotope), marble with nucleus (ie. an instance of a type of nucleus), and bag with sample. These percentages are the λ values - so the λ of the red marble, 10%, is 0.1.
Formal Inputs and Outputs
Input Description
You will be first given a value t. This is the number of seconds to run your calculations for - ie. calculate the percentages of the nuclei at this time.
You will be then given a line in the format:
a->b->c->...->zWhere a...z are types of nucleus. a is the starting nucleus type of your sample, and z is the end of the chain; the stable nucleus.
You will then be given lines in the format:
a: 0.0002Where a is an unstable type of nucleus, and 0.0002 is the λ constant for a. The last one in the decay chain must have a λ of zero (stable).
Output Description
You will print a line for all nuclei in the decay chain in the format:
a: 3.4%Where a is a type of nucleus in the decay chain, and 3.4% is the percentage of the sample that is nucleus type a after t seconds.
Sample Inputs and Outputs
Sample Input
5000
a->b->c->d->s
a: 0.00007
b: 0.0005
c: 0.00013
d: 0.00022
s: 0Sample Output 1
a: 70.37%
b: 10.25%
c: 15.00%
d: 3.31%
s: 1.07%Sample Output 2
a: 70.76%
b: 11.00%
c: 14.48%
d: 2.80%
s: 0.96%I'm using a random method so my results differ slightly (but are relatively consistent) each time. With a larger sample size your results will be more accurate. YMMV!
Extension
Plot the data as percentages over time! For the given input above, my test program gives this output for t=5000 s and this output for t=50000 s, where cooling colours are going down the decay chain. Make yours look really cool for extra points!
2
u/Eindacor_DS Oct 16 '14
For some reason I'm having a lot of trouble with this. When a nucleus decays into a daughter nucleus, is there no longer any remnant of the starting nucleus in the sample? So if the starting sample finally decays after 1000 seconds of a 5000-second sample, would that mean a: 80% is the output? I think I'm just having trouble wrapping my brain around the actual problem. Sorry if I'm a little slow, this is my first attempt at one of these.
2
u/skeeto -9 8 Oct 16 '14 edited Oct 16 '14
C, showing an animated simulation in the terminal using VT100 and ANSI escape codes.
Screenshot: http://i.imgur.com/7Hak2OW.png
1
u/Elite6809 1 1 Oct 15 '14
My solution in C#: https://gist.github.com/Quackmatic/5d1946502b30b1659033
Not the cleanest, but relatively fast on large input sets. Hacky workaround for sorting - was going to roll my own to get around .NET's horrifically slow string comparison but decided against it. My outputs and graphics are given in the post.
1
u/tiiv Oct 16 '14
What I don't understand here is this: let's say we have a sample size of 100 nuclei which all start out as type a nucleus. Now with a lamda value of 0.00007 that makes 0.007 nuclei that should decay into a type b nucleus. So basically none will decay after one second. Is the decay supposed to be accumulated over the seconds for each type until we reached a probability that allows for at least one nucleus to decay?
Sorry if this is a dumb question.
1
u/[deleted] Oct 16 '14
I seriously want to do this, but I can't understand it. At all. How do I know which sample size I have? And how I'll calculate the percentages?
Actually, this is very confusing, at least for me.
9
u/duetosymmetry Oct 16 '14
Physicist here. I'm commenting here because people are having trouble interpreting the problem statement, not because I'm going to provide a solution. I am going to try to restate it to give people another shot at trying to understand. (Use the TeX the world extension/plugin to see the math rendered right).
Radioactive decay proceeds through a Poisson process with a rate [; \lambda ;] (measured in units of 1/time). This means during each infinitesimal unit of time [; dt ;], each particle has a probability of [; \lambda,dt ;] of decaying. That means if you have a population of [; n ;] particles, after time [; dt ;], you expect to have a population of [; n - n \lambda dt ;] particles. Reinterpret this as the change [; dn ;] in number of particles is
or, as an ordinary differential equation,
Ok, now let's couple a number of species of particles together into a "reaction network". We will have species labeled by i=1, 2, ... k. At time t, each species has number
[; n_i(t) ;]. The rate at which species i is destroyed is still[; \lambda_i n_i(t) ;]. Now in general, some particle could decay into different reaction products. Here, however, we have the really simple situation that species 1 decays into species 2, species i decays into species i+1, etc. Thus the rate at which species i is created is[; +\lambda_{i-1} n_{i-1}(t) ;]. Put this together into the following vector ordinary differential equation:The first species (i=0) is special in this case. Just set
[; \lambda_{-1} = 0 = n_{-1} ;]and don't worry about it.So the problem is to integrate the above ODE system with initial conditions vector n = (1,0,...,0), for some total time T. Since it's an ODE integration, there is really no overhead to keep all the results from time t=0..T.
Please don't use Euler's method for integration, it sucks. Please at least use RK45. If you want to play with some fun stuff, try using an adaptive step size.
Question to think about: What choices of \lambda's will make it really difficult to integrate this ODE? Is there any way to deal with that?
3
u/adrian17 1 4 Oct 15 '14 edited Oct 15 '14
Python. Mathematical approach was tempting, but I preferred naive simulation for image creation. No randomness. Image uses random colors.
edit: changed drawing code a bit.
Image results: t=5000, t=50000
3
u/G33kDude 1 1 Oct 15 '14 edited Oct 16 '14
Done in AutoHotkey with hard-coded values. Variable input will be added in a bit.
Edit: Now with input parser. Just copy the input onto the clipboard and run the script
1
u/frozensunshine 1 0 Oct 15 '14
So to clarify, is this what you mean by the
lambdaconstant?2
u/pruby Oct 16 '14 edited Oct 16 '14
My Ruby solution.
2
u/fvandepitte 0 0 Oct 16 '14
C#
1
u/marchelzo Oct 15 '14 edited Oct 16 '14
EDIT: I decided to make it output images after all (performance is atrocious now, though). Examples below.
I don't think I did this right, but here is my attempt in Haskell.
I didn't do any random sampling, I just assumed that if a nucleus had associated with it a lambda value of, say, 0.3, then exactly 30% of nuclei of that type would decay. Also no pretty graphs. Overall this is pretty bad.
t = 1000
t = 25000
t = 50000
1
u/Godspiral 3 3 Oct 15 '14 edited Oct 15 '14
in J, input from clipboard
inp =. >({. , ". each @{: ) each ':' cut each cutLF wdclippaste ''
for 100k initial nuclei, remaining of each type (out of 100k)
plotting has straightforward extension of using boxed argument to power to generate the data set. I just don't have an imgur account.
simulating 10k molecules for 10k seconds:
20k seconds
1
u/pruby Oct 15 '14
Tried solving this mathematically with:
It produces very similar answers for the example case, but has a curious case of being undefined when any two decay rates in the chain are the same - in the second equation divides by the difference. Wondering if the one referenced there is a cumulative decay rate or such - anyone know?
1
u/jnazario 2 0 Oct 16 '14 edited Oct 16 '14
got it working in F#. i didn't fully grok what i had to do with ! and ref to get closures to work right with my mutable val amts but i did get it to work using a for loop. bleh. EDITED to update output.
the first sample input yields (for 100000 starting units):
1
u/[deleted] Oct 16 '14 edited Jan 02 '16
1
u/AtlasMeh-ed Oct 16 '14
I whipped up a java implementation that simulates randomness for each particle over a series of rounds. You can set the number of rounds to equal the time in order to get one-second-equals-one-round functionality or turn it up to see the effects of more continuous sampling.
1
u/Eindacor_DS Oct 16 '14 edited Oct 16 '14
Alright, this is my very first stab at one of these things, and I've probably re-written it 20 times by now (which is why it looks so convoluted and comment-less), but I'm tired of working on it and I think I finally got something. This is in C++, and for some reason my results are very consistent, though they don't match the samples given. If the base nucleus has a .007% chance to decay each second, and a test is run 5000 times, wouldn't that give each nucleus of the sample a 35% chance to have decayed? The code I came up with consistently hits around that mark for the first type of nucleus, not sure why the example keeps getting around 70% instead of 65%, but I realize my logic might be way off.
edit: added a few comments
1
u/squirrel-of-doom Oct 16 '14
Python, as I am currently trying to learn it.
1
u/tiiv Oct 16 '14 edited Oct 16 '14
C - my visualization is pretty lame
Input values:
Final output:
1
u/nuclearalchemist Oct 16 '14
Late to the party - done in Go using the Bateman's equations. Using a hack to deal with the cancellation on the last term in the series.
1
u/strHungarianNotation Oct 16 '14
JavaScript solution
1
u/YuEnDee14 Oct 17 '14
Here's my attempt in C#. I was planning on doing the image processing part of it today, but didn't get to it. I tested mine using the sample input and got similar results, though.
Hopefully I'll have time tomorrow to update it to generate an image, and if I do I'll update my Gist.
Feedback is welcome and appreciated! I don't do this sort of programming very often.
https://gist.github.com/YuEnDee14/817fe80f52efcb896e9e
1
u/Frigguggi 0 1 Oct 17 '14 edited Oct 18 '14
Edit: Added graphs, which, surprisingly, look a lot like those in the description, but a little smoother because I didn't use random sampling. Probably not the most efficient use of the awt and swing classes... I need practice with graphics.
Java:
Output:
5000
a->b->c->d->s
a: 0.00007
b: 0.0005
c: 0.00013
d: 0.00022
s: 0
Final state:
a: 70.47%
b: 10.14%
c: 15.10%
d: 3.20%
s: 1.10%
http://i.imgur.com/5IOx0bE.png
50000
a->b->c->d->s
a: 0.00007
b: 0.0005
c: 0.00013
d: 0.00022
s: 0
Final state:
a: 3.02%
b: 0.49%
c: 3.86%
d: 3.21%
s: 89.42%
http://i.imgur.com/ZgPPIzn.png
1
u/[deleted] Oct 17 '14
Used Python... no random element. Not sure why I would need to use one, thinking I might have partly misunderstood the prompt... but my outputs ended up in the problem's sample range when I used the sample input. Values hardcoded for the moment.
OUTPUT (reformatted):
1
u/KevinTheGray Oct 17 '14
C#
So this was a silly approach. I thought it would be cool to "model" the decay with Unity, so I tried doing that, and wellll, I mean it DOES work, but it doesn't really look good and I have spent way too much time on it. So, I am just going to post the two scripts I used and an output screenshot!
What is cool about it is you can watch in real time as the decay happens and you can also increase or decrease the timescale.
Here's an output of running for 5000 seconds with the sample input
1
u/lazydancer Oct 18 '14
That was fun. I made a d3 graph but had trouble updating it, might post it later
In javascript
1
u/ddsnowboard Oct 18 '14
Java. It's kinda sorta not too extremely slow. It did about 11 seconds for 100000 and about 1 for 10000. Constructive criticism is always appreciated, though I'm a little late to this whole party, as per usual.
1
u/Qyaffer Oct 20 '14 edited Oct 20 '14
Got the same result as some of the other posters, however the result does not change between different samples, so I guess the solution doesn't take into account randomness. I'm new here and relatively new to programming so I would appreciate feedback on my code. Thanks! And thank you Elite6809 for posting such a great challenge!
C++
OUTPUT
EDIT : I altered my decay method to have the following code :
However I get the following results :
Could someone help me understand what I am doing wrong?
1
u/CreatureKing Oct 21 '14
Originally written over multiple files.
Python
Output when t=5000 (hardcoded currently)
1
u/toodim Oct 22 '14
Python 3. Learning numpy arrays...
1
u/[deleted] Oct 28 '14
In Scheme: http://www.xojoc.pw/dailyprogrammer/radioactive-decay.html
1
u/MFreemans_Black_Hole Oct 16 '14
My solution in Java:
0
u/[deleted] Oct 16 '14
[deleted] — view deleted comment