Skip to content

Commit

Permalink
Solve p17 in fortran
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 2, 2024
1 parent 4d09a17 commit c9b0770
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Olivia's Project Euler Solutions
| | | | |CodeQL| |br| |
| | | | |C#-lint| |
+------------+----------------------------+--------+-------------------+
| Fortran | Fortran 95+ | 8 | |Fri| |
| Fortran | Fortran 95+ | 9 | |Fri| |
+------------+----------------------------+--------+-------------------+
| Java | Java 8+ in Corretto, |br| | 22 | |Javai| |br| |
| | Dragonwell, Liberica, |br| | | |Ja-Cov| |br| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`016`|:c-d:`0016`|:cp-d:`0016`|:cs-d:`0016`| |:ja-d:`0016`|:js-d:`0016`| |:py-d:`0016`|:rs-d:`0016`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`017`|:c-d:`0017`|:cp-d:`0017`|:cs-d:`0017`| |:ja-d:`0017`|:js-d:`0017`|:lu-d:`0017`|:py-d:`0017`|:rs-d:`0017`|
|:prob:`017`|:c-d:`0017`|:cp-d:`0017`|:cs-d:`0017`|:fr-d:`0017`|:ja-d:`0017`|:js-d:`0017`|:lu-d:`0017`|:py-d:`0017`|:rs-d:`0017`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`018`| | | | | |:js-d:`0018`| |:py-d:`0018`|:rs-d:`0018`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
14 changes: 14 additions & 0 deletions docs/src/fortran/p0017.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Fortran Implementation of Problem 17
====================================

View source code :source:`fortran/src/p0017.f90`

.. f:module:: Problem0017
.. f:function:: integer Problem0017/p0017()
.. literalinclude:: ../../../fortran/src/p0017.f90
:language: Fortran
:linenos:

.. tags:: word-problem, combinatorics
1 change: 1 addition & 0 deletions fortran/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ Problems Solved
- ☒ `9 <./src/p0009.f95>`__
- ☒ `11 <./src/p0011.f95>`__
- ☒ `13 <./src/p0013.f95>`__
- ☒ `17 <./src/p0017.f95>`__
- ☒ `836 <./src/p0836.f95>`__
77 changes: 77 additions & 0 deletions fortran/src/p0017.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
! Project Euler Problem 17
!
! The key here was remembering I don't need to return the whole string, just the length
!
! Problem:
!
! If the numbers 1 to 5 are written out in words: one, two, three, four, five,
! then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
!
! If all the numbers from 1 to 1000 (one thousand) inclusive were written out in
! words, how many letters would be used?
!
! NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
! forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
! letters. The use of "and" when writing out numbers is in compliance with
! British usage.

module Problem0017
implicit none
contains
integer recursive function to_string_len(n) result(answer)
integer, intent(in) :: n, tmp
if (n >= 1000) then
answer = to_string_len(n / 1000 % 100) + 8
if (n % 1000 /= 0) then
answer = answer + to_string_len(n % 1000)
end if

elseif (n >= 100) then
answer = to_string_len(n / 100 % 10) + 7
if (n % 100 /= 0) then
answer = answer + 3 + to_string_len(n % 100)
end if

elseif (n >= 20) then
select case (n / 10)
case (4, 5, 6)
answer = 5
case (2, 3, 8, 9)
answer = 6
case (7)
answer = 7
end select

if (n % 10 /= 0) then
answer = answer + to_string_len(n % 10)
end if

else
select case (n)
case (1, 2, 6, 10)
answer = 3
case (0, 4, 5, 9)
answer = 4
case (3, 7, 8)
answer = 5
case (11, 12)
answer = 6
case (15, 16)
answer = 7
case (13, 14, 18, 19)
answer = 8
case (17)
answer = 9
end select
end if
end function

integer function p0017() result(answer)
integer :: x

answer = 0
do x = 1, 1000
answer = answer + to_string_len(x)
end do
end function p0017
end module Problem0017
7 changes: 6 additions & 1 deletion fortran/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ program test
use Problem0009
use Problem0011
use Problem0013
use Problem0017
use Problem0836

implicit none
integer(i4t), dimension(:), allocatable :: problem_ids
logical, dimension(:), allocatable :: long_runtime
integer :: num_problems

num_problems = 8
num_problems = 9
allocate(problem_ids(num_problems))
allocate(long_runtime(num_problems))
if (.not. (allocated(problem_ids) .and. allocated(long_runtime))) then
Expand All @@ -30,6 +31,7 @@ program test
009_i4t, &
011_i4t, &
013_i4t, &
017_i4t, &
836_i4t &
/)
long_runtime = (/ &
Expand All @@ -40,6 +42,7 @@ program test
.false., &
.false., &
.false., &
.false., &
.false. &
/)

Expand Down Expand Up @@ -137,6 +140,8 @@ type(AnswerT) function select_function(problem_id) result(answer)
answer%int_value = p0011()
case (13)
answer%int_value = p0013()
case (17)
answer%int_value = p0017()
case (836)
allocate(character(len=14) :: answer%string_value)
if (.not. allocated(answer%string_value)) then
Expand Down

0 comments on commit c9b0770

Please sign in to comment.