# Golang : Calculate half life decay example

Tags : golang half-life-decay power-of math-pow round

My wife is a chemistry lecturer and she was showing her student how to calculate half-life decay of an element. For this tutorial, let's pretend that we are learning chemistry and learn how to calculate half-life decay of an element with a half-life of 25 years in Golang.

Solving half-life problems is focused on using several equations. The order in which you use them depends on **the data given** and **what is being asked**.

For our code example below, we are given:

-An element has a half-life of 25 years.

-The initial amount is 320 grams.

and asked :

-How many grams after 100 years ?

```
package main
import (
"fmt"
"math"
"time"
)
func round(input float64) float64 {
if input < 0 {
return math.Ceil(input - 0.5)
}
return math.Floor(input + 0.5)
}
func halfLifeDecay(startValue int, startTime time.Time, endTime time.Time, halfLifeInSeconds int) float64 {
timeSpan := endTime.Sub(startTime)
x := 2.0
y := timeSpan.Seconds() / float64(halfLifeInSeconds)
value := float64(startValue) / math.Pow(x, y)
// even if everything is gone(decayed)
// the left over is still 1 mathematically and by physics law
if value < 1 {
value += 1
}
value = round(value)
return value
}
func main() {
// From http://www.chemteam.info/Radioactivity/Radioactivity-Half-Life.html
fmt.Println("An element has a half-life of 25 years.")
fmt.Println("The initial amount is 320 grams.")
fmt.Println("How many grams after 100 years ? ")
// Simulate 100 years later
start := time.Date(2000, 11, 10, 20, 34, 58, 651387237, time.UTC)
end := time.Date(2100, 11, 10, 20, 34, 58, 651387237, time.UTC)
fmt.Println("-------------------------------------------------")
// Confirm that we are using the proper start and end dates
years := end.Year() - start.Year()
fmt.Println("Total years : ", years)
// we use 365 days, no leap or astronomical year
halfLifeInSeconds := 25 * (365 * (24 * 3600))
fmt.Println("Half Life in Seconds : ", halfLifeInSeconds)
fmt.Printf("Grams left after %v years is : %v grams.\n", years, halfLifeDecay(320, start, end, halfLifeInSeconds))
}
```

Output:

An element has a half-life of 25 years.

The initial amount is 320 grams.

How many grams after 100 years ?

Total years : 100

Half Life in Seconds : 788400000

Grams left after 100 years is : 20 grams.

If we change the years to 50 instead of 100:

An element has a half-life of 25 years.

The initial amount is 320 grams.

How many grams after 50 years ?

Total years : 50

Half Life in Seconds : 788400000

Grams left after 50 years is : 80 grams.

Happy decaying!

**References:**

https://www.socketloop.com/tutorials/golang-calculate-time-different

Formula from http://www.chemteam.info/Radioactivity/Radioactivity-Half-Life.html

#### See also : Golang : Calculations using complex numbers example

Tags : golang half-life-decay power-of math-pow round

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+1.9k AWS S3 : Prevent Hotlinking policy

+2.6k Golang : constant 20013 overflows byte error message

+1.7k Golang : Find network service name from given port and protocol

+5.3k Golang : How to get quoted string into another string?

+2.7k Golang : Load ASN1 encoded DSA public key PEM file example

+3.5k Golang : Simple word wrap or line breaking example

+12k Golang : How to validate URL the right way

+4.5k Golang : How to log each HTTP request to your web server?

+4.8k Golang : Wait and sync.WaitGroup example

+2.4k Golang : Get expvar(export variables) to work with multiplexer

+5.3k Golang : GUI with Qt and OpenCV to capture image from camera