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 a7b4c48 commit faeb526
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
17 changes: 10 additions & 7 deletions c/src/p0022.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 17 additions & 14 deletions cplusplus/src/p0022.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
25 changes: 14 additions & 11 deletions fortran/src/include/utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = ''
Expand Down

0 comments on commit faeb526

Please sign in to comment.