diff --git a/docs/src/fortran/p0010.rst b/docs/src/fortran/p0010.rst new file mode 100644 index 00000000..495d1f1a --- /dev/null +++ b/docs/src/fortran/p0010.rst @@ -0,0 +1,14 @@ +Fortran Implementation of Problem 10 +==================================== + +View source code :source:`fortran/src/p0010.f90` + +.. f:module:: Problem0010 + +.. f:function:: integer Problem0010/p0010() + +.. literalinclude:: ../../../fortran/src/p0010.f90 + :language: Fortran + :linenos: + +.. tags:: prime-number, fortran-iterator diff --git a/docs/src/fortran/p0022.rst b/docs/src/fortran/p0022.rst new file mode 100644 index 00000000..468e58d5 --- /dev/null +++ b/docs/src/fortran/p0022.rst @@ -0,0 +1,22 @@ +Fortran Implementation of Problem 22 +==================================== + +View source code :source:`fortran/src/p0022.f90` + +Includes +-------- + +- `utils.f90 <./lib/utils.html>`_ + +Problem Solution +---------------- + +.. f:module:: Problem0022 + +.. f:function:: integer Problem0022/p0022() + +.. literalinclude:: ../../../fortran/src/p0022.f90 + :language: Fortran + :linenos: + +.. tags:: word-problem, sorting, file-io diff --git a/fortran/src/p0010.f90 b/fortran/src/p0010.f90 new file mode 100644 index 00000000..3f79e813 --- /dev/null +++ b/fortran/src/p0010.f90 @@ -0,0 +1,24 @@ +! Project Euler Problem 10 +! +! Problem: +! +! The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. +! +! Find the sum of all the primes below two million. + +module Problem0010 + use constants + use primes + implicit none +contains + integer(i18t) function p0010() result(answer) + integer(i18t) :: tmp + + answer = 0 + tmp = 0 + do while (tmp < 2000000) + tmp = next_prime(tmp) + answer = answer + tmp + end do + end function p0010 +end module Problem0010 diff --git a/fortran/src/p0022.f90 b/fortran/src/p0022.f90 new file mode 100644 index 00000000..dfd72742 --- /dev/null +++ b/fortran/src/p0022.f90 @@ -0,0 +1,76 @@ +! Project Euler Problem 22 +! +! Problem: +! +! Using names.txt (right click and 'Save Link/Target As...'), a 46K text file +! containing over five-thousand first names, begin by sorting it into +! alphabetical order. Then working out the alphabetical value for each name, +! multiply this value by its alphabetical position in the list to obtain a name +! score. +! +! For example, when the list is sorted into alphabetical order, COLIN, which is +! worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would +! obtain a score of 938 × 53 = 49714. +! +! What is the total of all the name scores in the file? + +module Problem0022 + use constants + use utils + implicit none + integer, parameter :: name_count = 5163 + integer, parameter :: longest_name = 11 +contains + integer(i18t) function p0022() result(answer) + character(len=longest_name), dimension(name_count) :: names + character(len=1) current_char + integer(i18t) score + integer ios, unit, i, j + + i = 1 + answer = 0 + names = '' + unit = open_data_file("p0022_names.txt") + do + read(unit, '(A1)', IOSTAT=ios) current_char + if (ios /= 0) then + exit + end if + + select case current_char + case (',') + i = i + 1 + case ('"') + continue + case default + names(i) = names(i) // current_char + end select + end do + close(unit) + call p0022_sort(names) + do i = 1, name_count + score = 0 + do j = 1, len(names(i)) + score = score + (ichar(names(i)(j)) .and. 63) + end do + answer = answer + score * i + end do + end function p0022 + + subroutine p0022_sort(names) + character(len=longest_name), dimension(name_count), intent(inout) :: names + character(len=longest_name) :: temp + integer i, j + + ! bubble sort, because we don't need anything fancier for this program + do i = 1, size(names) + do j = 1, size(names) - i + if (names(j) > names(j + 1)) then + temp = names(j) + names(j) = names(j + 1) + names(j + 1) = temp + end if + end do + end do + end subroutine +end module Problem0022