Skip to content

Commit

Permalink
Solve p22 in fortran; fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 7, 2024
1 parent faeb526 commit 2082640
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/src/fortran/p0010.rst
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions docs/src/fortran/p0022.rst
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions fortran/src/p0010.f90
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions fortran/src/p0022.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2082640

Please sign in to comment.