-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.txt
671 lines (613 loc) · 15.7 KB
/
output.txt
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
**task: Write a Fortran program to compute Euler's number using a Taylor series with the number of terms
`nterms` equal to 1000. Set `integer, parameter :: dp = kind(1.0d0)` and declare real variables
as `real(kind=dp)`. Use :: in declarations. Use implicit none and make sure to declare
all variables. You MUST use ```fortran to show the start of Fortran code.**
attempt 1
program euler
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: nterms
real(kind=dp) :: euler, term, denominator
integer :: factorial, i, j
nterms = 1000
euler = 0.0_dp
do i = 0, nterms-1
factorial = 1
denominator = 1.0_dp
do j = 1, i
factorial = factorial * j
denominator = denominator * (i + 1.0_dp)
end do
term = 1.0_dp * factorial / denominator
euler = euler + term
end do
print *, "Euler's number is:", euler
end program euler
Compilation failed with the following output:
temp.f90:5:24:
5 | real(kind=dp) :: euler, term, denominator
| 1
Error: Symbol 'euler' at (1) cannot have a type
temp.f90:8:8:
8 | euler = 0.0_dp
| 1
Error: 'euler' at (1) is not a variable
temp.f90:17:10:
17 | euler = euler + term
| 1
Error: 'euler' at (1) is not a variable
temp.f90:19:38:
19 | print *, "Euler's number is:", euler
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f90:11:15:
11 | denominator = 1.0_dp
| 1
Error: Symbol 'denominator' at (1) has no IMPLICIT type
temp.f90:16:8:
16 | term = 1.0_dp * factorial / denominator
| 1
Error: Symbol 'term' at (1) has no IMPLICIT type; did you mean 'nterms'?
attempt 2
program euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: nterms, i
real(dp) :: term, sum, factorial
nterms = 1000
sum = 0.0_dp
do i = 0, nterms
factorial = 1.0_dp
term = 1.0_dp
do while (i >= 1)
factorial = factorial * i
i = i - 1
end do
do while (i <= nterms - 1)
term = term / real(i+1, kind=dp)
i = i + 1
end do
sum = sum + term / factorial
end do
write(*,*) "Euler's number: ", sum
end program euler_number
Compilation failed with the following output:
temp.f90:13:17:
8 | do i = 0, nterms
| 2
......
13 | i = i - 1
| 1
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)
temp.f90:17:17:
8 | do i = 0, nterms
| 2
......
17 | i = i + 1
| 1
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)
attempt 3
program euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: e, fact, term
integer :: i, nterms
! Set number of terms
nterms = 1000
! Compute Euler's number using Taylor series
e = 1.0_dp
fact = 1.0_dp
do i = 1, nterms
fact = fact * i
term = 1.0_dp / fact
e = e + term
end do
! Print result
write(*,*) "Euler's number = ", e
end program euler_number
output:
Euler's number = 2.7182818284590455
attempt 4
program euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: n, nterms
real(kind=dp) :: euler, term
nterms = 1000
euler = 1.0_dp ! initialize euler to 1
term = 1.0_dp
do n = 1, nterms
term = term / n ! compute the next term in the Taylor series
euler = euler + term ! add the new term to the sum
end do
write(*,*) "Euler's number is", euler
end program euler_number
output:
Euler's number is 2.7182818284590455
attempt 5
program euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, nterms = 1000
real(kind=dp) :: euler, term, fact
euler = 1.0_dp
term = 1.0_dp
do i = 1, nterms
fact = 1.0_dp
do j = 1, i
fact = fact * j
end do
term = term / fact
euler = euler + term
end do
write(*,*) "Euler's number is: ", euler
end program euler_number
Compilation failed with the following output:
temp.f90:10:9:
10 | do j = 1, i
| 1
Error: Symbol 'j' at (1) has no IMPLICIT type
attempt 6
program compute_euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, nterms
real(kind=dp) :: e, fact, term
nterms = 1000
e = 1.0_dp
do i = 1, nterms
fact = 1.0_dp
do j = 2, i
fact = fact * j
end do
term = 1.0_dp / fact
e = e + term
end do
write(*,*) 'Euler Number =', e
end program compute_euler_number
Compilation failed with the following output:
temp.f90:10:8:
10 | do j = 2, i
| 1
Error: Symbol 'j' at (1) has no IMPLICIT type
attempt 7
program compute_euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
real(dp) :: euler_num = 1.0_dp, term = 1.0_dp
integer :: i, nterms = 1000
do i = 1, nterms
term = term / i
euler_num = euler_num + term
end do
write(*,*) "Euler's number is ", euler_num
end program compute_euler_number
output:
Euler's number is 2.7182818284590455
attempt 8
program compute_eulers_number
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, nterms
real(kind=dp) :: e, term, factorial
nterms = 1000
e = 1.0_dp
term = 1.0_dp
factorial = 1.0_dp
do i = 1, nterms
factorial = factorial * real(i, dp)
term = term / factorial
e = e + term
end do
print *, "Euler's number: ", e
end program compute_eulers_number
output:
Euler's number: 2.5868345309364718
attempt 9
program euler_number
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) euler, term
integer i, nterms
nterms = 1000
euler = 1.0_dp
term = 1.0_dp
do i = 1, nterms
term = term / real(i, kind=dp)
euler = euler + term
end do
write(*,*) "Euler's number is: ", euler
end program euler_number
output:
Euler's number is: 2.7182818284590455
attempt 10
program compute_euler
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: n
real(kind=dp) :: e, term, fact
e = 1.0_dp
term = 1.0_dp
fact = 1.0_dp
do n = 1, 1000
fact = fact * n
term = term / fact
e = e + term
end do
write(*,*) "Euler's number with 1000 terms:", e
end program compute_euler
output:
Euler's number with 1000 terms: 2.5868345309364718
**task: Write a Fortran program to compute the prime numbers below 50. Set `integer, parameter :: dp = kind(1.0d0)` and declare real variables
as `real(kind=dp)`. Use :: in declarations. Use implicit none and make sure to declare
all variables. You MUST use ```fortran to show the start of Fortran code.**
attempt 1
program primes
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: num, factor
integer :: i, j, is_prime
write(*,*) 'Prime numbers below 50:'
do i = 2, 50
num = i
is_prime = 1
do j = 2, int(sqrt(num))
factor = j
if (mod(num,factor) == 0) then
is_prime = 0
exit
endif
enddo
if (is_prime == 1) then
write(*,*) num
endif
enddo
end program primes
!!
!! Note: We use the `sqrt` function from the `int` intrinsic module to find the square root of `num` and the `mod` intrinsic function to check if `factor` divides `num`. The loop is exited if a factor is found, as `num` is not a prime. In the main program we simply loop through all integers between 2 and 50 and call a subprogram to determine if each is prime. If the subprogram returns true, we output the integer.
output:
Prime numbers below 50:
2.0000000000000000
3.0000000000000000
5.0000000000000000
7.0000000000000000
11.000000000000000
13.000000000000000
17.000000000000000
19.000000000000000
23.000000000000000
29.000000000000000
31.000000000000000
37.000000000000000
41.000000000000000
43.000000000000000
47.000000000000000
attempt 2
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, j
real(kind=dp) :: sqrt_n
logical :: is_prime
do i = 2, 50
is_prime = .true.
sqrt_n = sqrt(real(i, kind=dp))
do j = 2, i-1
if (mod(i,j) == 0) then
is_prime = .false.
exit
elseif (real(j,kind=dp) > sqrt_n) then
exit
end if
end do
if (is_prime) then
write(*,*) i
end if
end do
end program prime_numbers
output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
attempt 3
program primes_below_50
implicit none
integer, parameter :: dp = kind(1.0d0)
integer, parameter :: n = 50
integer :: i, j, is_prime
real(kind=dp) :: numbers(n)
numbers = [(i, i=2,n)]
do i=2,n
is_prime = 1
do j=2,i-1
if (mod(i,j) == 0) then
is_prime = 0
exit
end if
end do
if (is_prime == 1) then
write(*,*) numbers(i)
end if
end do
end program primes_below_50
Compilation failed with the following output:
temp.f90:7:2:
7 | numbers = [(i, i=2,n)]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (50 and 49)
attempt 4
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, j, n
real(kind=dp) :: sqrt_n
logical :: is_prime
do n = 2, 50
sqrt_n = sqrt(real(n, kind=dp))
is_prime = .true.
do i = 2, n-1
if (mod(n, i) == 0) then
is_prime = .false.
exit
end if
if (real(i, kind=dp) > sqrt_n) then
exit
end if
end do
if (is_prime) then
write(*,*) n
end if
end do
end program prime_numbers
output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
attempt 5
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: i, j
integer :: n
do n = 2, 50
do i = 2, n-1
j = n/i
if (j == int(j)) then
exit
end if
end do
if (i == n-1) then
print *, n
end if
end do
end program prime_numbers
output:
attempt 6
program primes
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: n, i
real(kind=dp) :: sqrt_n
logical :: is_prime
print*, "Prime numbers less than 50:"
do n = 2, 49
is_prime = .true.
sqrt_n = sqrt(real(n, dp))
do i = 2, int(sqrt_n)
if (mod(n, i) == 0) then
is_prime = .false.
exit
endif
end do
if (is_prime) print*, n
end do
end program primes
!!
!! Note: The program checks whether each number n is a prime by checking whether it is divisible by any integer from 2 to int(sqrt(n)). If it is not divisible by any of these integers, then it is a prime number.
output:
Prime numbers less than 50:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
attempt 7
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: num
integer :: i, j
logical :: is_prime
do i = 2, 50
num = real(i, dp)
is_prime = .true.
do j = 2, i-1
if (mod(i,j) == 0) then
is_prime = .false.
exit
end if
end do
if (is_prime) then
write(*,*) 'Prime number: ', i
end if
end do
end program prime_numbers
output:
Prime number: 2
Prime number: 3
Prime number: 5
Prime number: 7
Prime number: 11
Prime number: 13
Prime number: 17
Prime number: 19
Prime number: 23
Prime number: 29
Prime number: 31
Prime number: 37
Prime number: 41
Prime number: 43
Prime number: 47
attempt 8
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: candidate, divisor
integer :: i
do i = 2, 50
candidate = i
if (is_prime(candidate)) then
write(*,*) candidate
endif
end do
contains
function is_prime(n) result(prime)
integer :: n, i
logical :: prime
prime = .TRUE.
do i = 2, n-1
divisor = i
if (mod(n,divisor) == 0) then
prime = .FALSE.
exit
end if
end do
end function is_prime
end program prime_numbers
!!
!! Note: This program uses a function called `is_prime` to determine if a given number is prime. This function returns `True` if the number is prime, and `False` otherwise. The `mod` operator is used to check if the candidate number is divisible by any number between 2 and n-1. If the modulo operation returns 0, then the candidate is not prime and the function returns `False`. If the function reaches the end of the loop without finding any divisors, then the candidate is prime and the function returns `True`.
Compilation failed with the following output:
temp.f90:19:16:
19 | if (mod(n,divisor) == 0) then
| 1
Error: 'a' and 'p' arguments of 'mod' intrinsic at (1) must have the same type
temp.f90:8:8:
8 | if (is_prime(candidate)) then
| 1
Error: Type mismatch in argument 'n' at (1); passed REAL(8) to INTEGER(4)
attempt 9
program prime
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i, j
real(kind=dp) :: number
logical :: is_prime
write(*,*) "List of prime numbers below 50:"
do i = 2, 50
is_prime = .true.
number = real(i,dp)
do j = 2, i-1
if (mod(i,j)==0) then
is_prime = .false.
exit
end if
end do
if (is_prime) write(*,*) i
end do
end program prime
!!
!! Output:
!! List of prime numbers below 50:
!! 2
!! 3
!! 5
!! 7
!! 11
!! 13
!! 17
!! 19
!! 23
!! 29
!! 31
!! 37
!! 41
!! 43
!! 47
output:
List of prime numbers below 50:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
attempt 10
program prime_numbers
implicit none
integer, parameter :: dp = kind(1.0d0)
integer i, j, n, count
real(kind=dp) sqrtN
do n = 2, 50
sqrtN = sqrt(real(n))
count = 0
do i = 2, n-1
if (mod(n,i) .eq. 0) then
count = count+1
exit
end if
end do
if (count == 0) then
write(*,'(I3)') n
end if
end do
end program prime_numbers
output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
time elapsed (sec): 170.03 for 2 tasks and 10 tries each.