-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d09a17
commit c9b0770
Showing
6 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters