# The birthday paradox: help to be faster than Fortran

**URL:** <https://discourse.julialang.org/t/the-birthday-paradox-help-to-be-faster-than-fortran/44809>\
**Category:** Performance\
**Tags:** fortran, unrolling\
**Created:** [August 12, 2020, 2:28pm UTC](https://discourse.julialang.org/t/the-birthday-paradox-help-to-be-faster-than-fortran/44809 "2020-08-12T14:28:58Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [August 12, 2020, 6:20pm UTC](https://discourse.julialang.org/t/the-birthday-paradox-help-to-be-faster-than-fortran/44809/10 "2020-08-12T18:20:50Z")

</div>

Awkward, but I do get better performance out of this:

```julia
julia> @generated function same_day(v, ::Val{n_people}) where {n_people}
           q = Expr(:block); for i = 1:n_people-1
               for j = i+1:n_people
                   push!(q.args, :(v[$i] == v[$j] && return true))
               end
           end
           quote
               $(Expr(:meta,:inline))
               @inbounds begin
                   $q
               end
               false
           end
       end
same_day (generic function with 3 methods)

julia> @inline function add1_recursive(v, p, n_people, n_days)
           # `v` is the vector with birthdays
           # `p` is the index position that must be increased v[p]+=1
           @inbounds while true;
           if p < 1
             return
           elseif v[p] != n_days
               v[p] += 1; return
           else
               v[p] = 1
               p -= 1
           end
           end
       end
add1_recursive (generic function with 1 method)

```

You then need to call `same_day(v, Val(n_people))` inside `main`.

With this, I got:

```julia
Coincidences 965497600/10000000000=9.654976%
 20.213654 seconds (1.15 k allocations: 35.359 KiB)

```

Versus `gfortran`:

```nohighlight
Coincidences 965497600 / 10000000000 = 9.65%

________________________________________________________
Executed in 16.86 secs fish external
   usr time 16.85 secs 229.00 micros 16.85 secs
   sys time 0.00 secs 76.00 micros 0.00 secs

```

---

_[View the full topic](https://discourse.julialang.org/t/the-birthday-paradox-help-to-be-faster-than-fortran/44809)._
