-
Notifications
You must be signed in to change notification settings - Fork 5
/
euler.for
169 lines (145 loc) · 3.94 KB
/
euler.for
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
C Project Euler in Fortran
C John Evans <[email protected]>
PROGRAM EULER
IMPLICIT NONE
INTEGER:: EULER1, EULER2, EULER3, EULER4
PRINT*, EULER1()
PRINT*, EULER2()
PRINT*, EULER3()
PRINT*, EULER4()
STOP
END
C Euler #1
C Answer: 233168
C
C If we list all the natural numbers below 10 that are multiples of 3 or 5,
C we get 3, 5, 6 and 9. The sum of these multiples is 23.
C
C Find the sum of all the multiples of 3 or 5 below 1000.
INTEGER FUNCTION EULER1 ()
INTEGER :: I
EULER1 = 0
DO I = 3, 999
IF (MODULO(I, 3) .EQ. 0 .OR. MODULO(I, 5) .EQ. 0) THEN
EULER1 = EULER1 + I
END IF
END DO
RETURN
END
C Euler #2
C Answer: 4613732
C
C Each new term in the Fibonacci sequence is generated by adding the previous
C two terms. By starting with 1 and 2, the first 10 terms will be:
C
C 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
C
C Find the sum of all the even-valued terms in the sequence which do not
C exceed four million.
INTEGER FUNCTION EULER2 ()
IMPLICIT NONE
INTEGER :: LAST, NEXT, CUR
EULER2 = 2
LAST = 1
CUR = 2
DO WHILE (CUR < 4000000)
NEXT = LAST + CUR
IF (MODULO(NEXT, 2) .EQ. 0) THEN
EULER2 = EULER2 + NEXT
END IF
LAST = CUR
CUR = NEXT
END DO
RETURN
END
C Euler #3:
C Answer: 6857
C Pretty damn slow for how awesome Fortran is supposed to be at numerical
C work, but oh well.
C
C The prime factors of 13195 are 5, 7, 13 and 29.
C
C What is the largest prime factor of the number 600851475143 ?
LOGICAL FUNCTION ISPRM(N)
IMPLICIT NONE
INTEGER :: N, I
LOGICAL :: DONE
DONE = .FALSE.
I = CEILING(SQRT(REAL(N)))
ISPRM = .TRUE.
DO WHILE (.NOT. DONE)
IF (MODULO(N, I) .EQ. 0) THEN
ISPRM = .FALSE.
DONE = .TRUE.
END IF
I = I - 1
IF (I < 2) THEN
DONE = .TRUE.
END IF
END DO
RETURN
END
INTEGER FUNCTION EULER3()
IMPLICIT NONE
INTEGER :: I, T, MAX_FACTOR
LOGICAL :: DIVIS, PRIME, ISPRM
T = 600851475143
MAX_FACTOR = CEILING(SQRT(REAL(T)))
DO I = MAX_FACTOR, 2, -1
DIVIS = MODULO(T, I) .EQ. 0
PRIME = ISPRM(I)
IF (DIVIS .AND. PRIME) THEN
EULER3 = I
RETURN
END IF
END DO
EULER3 = -1
RETURN
END
C Problem #4
C Answer: 906609
C
C A palindromic number reads the same both ways. The largest
C palindrome made from the product of two 2-digit numbers is 9009 =
C 91 99.
C
C Find the largest palindrome made from the product of two 3-digit
C numbers.
C SUBROUTINE BACKWRDS(STR)
C CHARACTER STR(10)
C CHARACTER N
C
C J = 10
C DO 100 K = 1,5
C N = STR(K)
C STR(K) = STR(J)
C STR(J) = N
C J = J - 1
C 100 CONTINUE
C RETURN
C END
LOGICAL FUNCTION ISPAL(N)
IMPLICIT NONE
INTEGER :: N
CHARACTER :: S(32), R(32)
WRITE(S, '(I10)') N
R = BACKWARDS(S)
ISPAL = S .EQ. R
RETURN
END
INTEGER FUNCTION EULER4()
IMPLICIT NONE
INTEGER :: I, J, P, RESULT
LOGICAL ISPAL
RESULT = 0
DO I = 100, 999, 1
DO J = 100, 999, 1
P = I * J
IF ((P .GT. RESULT) .AND. (ISPAL(P))) THEN
RESULT = P
END IF
END DO
END DO
EULER4 = P
RETURN
END