A Norwegian Birth Number consists of 11 digits, composed the following way:
DDMMYYiiikk
DD
is the day (from 01-31)MM
is the month (from 01-12)YY
is the year (from 00-99). It's not differentiated between 1900 and 2000iii
is the "individual number"kk
are two control digits
iii
is determined by birthyear and gender the following way
- 0000-1900: Disregard, there is some inconsistency and special cases
- 1900-1999: Range = 000-499
- 2000-2039: Range = 500-999
- Female: Even numbers (and 000)
- Male: Odd numbers
The control numbers are determined the following way:
Let's call the 11 digits:
d1 d2 m1 m2 y1 y2 i1 i2 i3 k1 k2
Then the control digits can be calculated using the equations:
k1 = 11 - ((3 * d1 + 7 * d2 + 6 * m1 + 1 * m2 + 8 * y1 + 9 * y2 + 4 * i1 + 5 * i2 + 2 * i3) mod 11)
k2 = 11 - ((5 * d1 + 4 * d2 + 3 * m1 + 2 * m2 + 7 * y1 + 6 * y2 + 5 * i1 + 4 * i2 + 3 * i3 + 2 * k1) mod 11).
For some combinations, the control numbers k1
or k2
can become 10
. If that's the case, the number will be invalid.
If the sum modulus 11 for k1
or k2
is 11, i.e. k1 = 11 - (11 mod 11)
, then the control digit will be 0, not 11.
Challenge
Take a letter, M
or F
(male or female), and an eleven digit number as input, and check if the Birth Number is valid according to the rules above.
- Input format and order is optional
- The 11 numbers must be a single number or a consecutive string (you can't take the input as
DD, MM, YY, iii, kk
). - You can assume the date is valid (310699xxxxx will not be given as input)
- Output is a truthy/falsy value (1/0, true/false etc.)
- Program or function
- All standard rules apply
You can find all valid numbers on this page (in Norwegian) by choosing a date.
Examples:
M, 01010099931
True
F, 01029042620
True
M, 0101009841
False
F, 01010051866
True
F, 08021690849
True
M, 01029040105
True
M, 01029037473
False
Shortest code in bytes win.
["Q", "01010099931"]
returnstrue
?) \$\endgroup\$