How to resolve the algorithm Benford's law step by step in the Fortran programming language
How to resolve the algorithm Benford's law step by step in the Fortran programming language
Table of Contents
Problem Statement
Benford's law, also called the first-digit law, refers to the frequency distribution of digits in many (but not all) real-life sources of data. In this distribution, the number 1 occurs as the first digit about 30% of the time, while larger numbers occur in that position less frequently: 9 as the first digit less than 5% of the time. This distribution of first digits is the same as the widths of gridlines on a logarithmic scale. Benford's law also concerns the expected distribution for digits beyond the first, which approach a uniform distribution. This result has been found to apply to a wide variety of data sets, including electricity bills, street addresses, stock prices, population numbers, death rates, lengths of rivers, physical and mathematical constants, and processes described by power laws (which are very common in nature). It tends to be most accurate when values are distributed across multiple orders of magnitude. A set of numbers is said to satisfy Benford's law if the leading digit
d
{\displaystyle d}
(
d ∈ { 1 , … , 9 }
{\displaystyle d\in {1,\ldots ,9}}
) occurs with probability For this task, write (a) routine(s) to calculate the distribution of first significant (non-zero) digits in a collection of numbers, then display the actual vs. expected distribution in the way most convenient for your language (table / graph / histogram / whatever). Use the first 1000 numbers from the Fibonacci sequence as your data set. No need to show how the Fibonacci numbers are obtained. You can generate them or load them from a file; whichever is easiest. Display your actual vs expected distribution.
For extra credit: Show the distribution for one other set of numbers from a page on Wikipedia. State which Wikipedia page it can be obtained from and what the set enumerates. Again, no need to display the actual list of numbers or the code to load them.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Benford's law step by step in the Fortran programming language
Source code in the fortran programming language
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat May 18 01:13:00
a=./f && make $a && $a
f95 -Wall -ffree-form f.F -o f
0.301030010 0.176091254 0.124938756 9.69100147E-02 7.91812614E-02 6.69467747E-02 5.79919666E-02 5.11525236E-02 4.57575098E-02 THE LAW
0.300999999 0.177000001 0.125000000 9.60000008E-02 7.99999982E-02 6.70000017E-02 5.70000000E-02 5.29999994E-02 4.50000018E-02 LEADING FIBONACCI DIGIT
Compilation finished at Sat May 18 01:13:00
subroutine fibber(a,b,c,d)
! compute most significant digits, Fibonacci like.
implicit none
integer (kind=8), intent(in) :: a,b
integer (kind=8), intent(out) :: c,d
d = a + b
if (15 .lt. log10(float(d))) then
c = b/10
d = d/10
else
c = b
endif
end subroutine fibber
integer function leadingDigit(a)
implicit none
integer (kind=8), intent(in) :: a
integer (kind=8) :: b
b = a
do while (9 .lt. b)
b = b/10
end do
leadingDigit = transfer(b,leadingDigit)
end function leadingDigit
real function benfordsLaw(a)
implicit none
integer, intent(in) :: a
benfordsLaw = log10(1.0 + 1.0 / a)
end function benfordsLaw
program benford
implicit none
interface
subroutine fibber(a,b,c,d)
implicit none
integer (kind=8), intent(in) :: a,b
integer (kind=8), intent(out) :: c,d
end subroutine fibber
integer function leadingDigit(a)
implicit none
integer (kind=8), intent(in) :: a
end function leadingDigit
real function benfordsLaw(a)
implicit none
integer, intent(in) :: a
end function benfordsLaw
end interface
integer (kind=8) :: a, b, c, d
integer :: i, count(10)
data count/10*0/
a = 1
b = 1
do i = 1, 1001
count(leadingDigit(a)) = count(leadingDigit(a)) + 1
call fibber(a,b,c,d)
a = c
b = d
end do
write(6,*) (benfordsLaw(i),i=1,9),'THE LAW'
write(6,*) (count(i)/1000.0 ,i=1,9),'LEADING FIBONACCI DIGIT'
end program benford
You may also check:How to resolve the algorithm Sleep step by step in the Retro programming language
You may also check:How to resolve the algorithm Maze generation step by step in the XPL0 programming language
You may also check:How to resolve the algorithm CUSIP step by step in the BASIC programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Special characters step by step in the Mathematica/Wolfram Language programming language