diff --git a/c/src/p0022.c b/c/src/p0022.c index 400d1470..d954c698 100644 --- a/c/src/p0022.c +++ b/c/src/p0022.c @@ -1,16 +1,19 @@ /* Project Euler Problem 22 -I know that this could be done faster with a traditional for loop, but I wanted -to see if iterators were reasonably possible in C, since it makes the prime -number infrastructure a lot easier to set up. - Problem: -If we list all the natural numbers below 10 that are multiples of 3 or 5, we -get 3, 5, 6 and 9. The sum of these multiples is 23. +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. -Find the sum of all the multiples of 3 or 5 below 1000. +What is the total of all the name scores in the file? */ #ifndef EULER_P0022 #define EULER_P0022 diff --git a/cplusplus/src/p0022.cpp b/cplusplus/src/p0022.cpp index 0e4665e1..70de57ee 100644 --- a/cplusplus/src/p0022.cpp +++ b/cplusplus/src/p0022.cpp @@ -1,17 +1,20 @@ -/** - * Project Euler Problem 22 - * - * I know that this could be done faster with a traditional for loop, but I wanted - * to see if iterators were reasonably possible in C, since it makes the prime - * number infrastructure a lot easier to set up. - * - * Problem: - * - * If we list all the natural numbers below 10 that are multiples of 3 or 5, we - * get 3, 5, 6 and 9. The sum of these multiples is 23. - * - * Find the sum of all the multiples of 3 or 5 below 1000. - */ +/* +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? +*/ #ifndef EULER_P0022 #define EULER_P0022 #include diff --git a/docs/index.rst b/docs/index.rst index 986c861a..6dfcd501 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -120,7 +120,7 @@ Problems Solved +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`021`| | | | | |:js-d:`0021`| |:py-d:`0021`|:rs-i:`0021`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ - |:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`| |:ja-d:`0022`|:js-d:`0022`| |:py-d:`0022`|:rs-d:`0022`| + |:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`|:fr-d:`0022`|:ja-d:`0022`|:js-d:`0022`| |:py-d:`0022`|:rs-d:`0022`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`023`| | | | | |:js-d:`0023`| |:py-d:`0023`|:rs-d:`0023`| +-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/fortran/src/include/utils.f90 b/fortran/src/include/utils.f90 index 1f086dac..32eed6d2 100644 --- a/fortran/src/include/utils.f90 +++ b/fortran/src/include/utils.f90 @@ -12,6 +12,19 @@ module utils logical, private :: cache_inited = .false. type(AnswerT), private, dimension(1024) :: cached_answers contains + integer function open_data_file(name) result(unit) + character(len=32), intent(in) :: name + integer :: ios + + unit = prev_unit + 1 + prev_unit = unit + open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios) + if (ios /= 0) then + print *, "Error opening file: ../_data/" // name + return + end if + end function + type(AnswerT) function get_answer(id) result(answer) integer(i4t), intent(in) :: id @@ -36,17 +49,7 @@ subroutine init_answer_cache() integer :: ios, line_length, unit_number cached_answers = AnswerT(0, '', errort) - do i=1, size(cached_answers) - end do - - unit_number = prev_unit + 1 - prev_unit = unit_number - open(unit=unit_number, file=("../_data/answers.tsv"), status='old', action='read', iostat=ios) - if (ios /= 0) then - print *, "Error opening file: ../_data/answers.tsv" - return - end if - + unit_number = open_data_file("answers.tsv") line_length = 1 do while (line_length > 0) line = ''