Commit 4e1ae314 authored by Bruce Cowan's avatar Bruce Cowan

Add fortran implementations of the first two problems

parent 2c1dab4a
executable('prob1f', 'prob1.f90')
executable('prob2f', 'prob2.f90')
program prob1
implicit none
integer :: a, b
integer :: lower, upper
integer :: total = 0
print *, "A?"
read (*, *) a
print *, "B?"
read (*, *) b
lower = min(a, b)
upper = max(a, b)
total = get_mults_35(lower, upper)
print '(a, i0, a, i0, a, i0)', "The sum of the multiples of 3 or 5 over [", &
lower, ", ", upper, ") is ", total
contains
integer function get_mults_35(lower, upper) result(total)
integer, intent(in) :: lower, upper
integer :: i
total = 0
do i = lower, upper - 1
if ((mod(i, 3) == 0) .or. (mod(i, 5) == 0)) then
total = total + i
end if
end do
end function get_mults_35
end program prob1
program prob2
implicit none
integer, dimension(1000) :: fibs = 0
integer :: target
integer :: current
integer :: total = 0
integer :: length
integer :: i
! Set up array
fibs(1:2) = [1, 2]
do i = 3, 1000
fibs(i) = 0
enddo
length = 2
! Get target
print *, "Target?"
read (*, *) target
do
current = fibs(length-1) + fibs(length)
if (current > target) then
exit
end if
fibs(length+1) = current
length = length + 1
end do
do i = 1, length
if (mod(fibs(i), 2) == 0) then
total = total + fibs(i)
end if
end do
print '(a, i0, a, i0)', "Sum of even Fibonacci numbers under ", target, &
" is ", total
end program prob2
project('euler', 'c')
project('euler', ['c', 'fortran'])
add_global_arguments('-Wextra', language : 'c')
inc = include_directories('lib')
subdir('fortran')
subdir('lib')
subdir('src')
subdir('test')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment