Skip to content

Commit 3fa547d

Browse files
authored
Modify Fibonacci in Fortran (#5123)
Improve Fibonacci Fortran code snippet
1 parent f85d0b4 commit 3fa547d

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

archive/f/fortran/fibonacci.f95

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
1-
! In program name, - is not allowed
2-
! works until 184 (Stop is not implemented)
31
program fibonacci
4-
5-
! Create the variables
6-
character(26) :: low = 'abcdefghijklmnopqrstuvwxyz' ! Defines all lowercase letters. This is used to later scan for letters in the input as a test.
7-
character(26) :: cap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ! Defines all uppercase letters. This is used to later scan for letters in the input as a test.
8-
integer(kind = 16) :: previousnumber, currentnumber, addednumber, uppers, lowers, loop
9-
character(len=10) :: argument ! Defines the object we will be recieving. It will arrive as a charcters as the expectation is keyboard input.
10-
! Fortran knows that 'argument' is user input, so we never need to set it to anything
11-
12-
! Define variables.
13-
previousnumber = 0
14-
currentnumber = 1
15-
addednumber = 0
16-
17-
IF(COMMAND_ARGUMENT_COUNT().NE.1)THEN
18-
write(*,'(g0.8)')"Usage: please input the count of fibonacci numbers to output"
19-
STOP
20-
ENDIF
21-
22-
CALL GET_COMMAND_ARGUMENT(1,argument)
23-
if (argument == "") then
24-
write(*,'(g0.8)')"Usage: please input the count of fibonacci numbers to output"
25-
STOP
26-
endif
27-
28-
! Tests to make sure we have recieved NUMBERS and not anything else. These act as flags that will later end the program.
29-
uppers = scan(argument, cap) ! This will look for uppercase letters in the recieved string. If any letter is uppercase, uppers will be updated.
30-
lowers = scan(argument, low) ! This will look for lowercase letters in the recieved string. If any letter is lowercase, lowers will be updated.
31-
32-
! Will see if there are any uppercase, if so, print an error.
33-
if (uppers > 0) then
34-
write(*,'(g0.8)')"Usage: please input the count of fibonacci numbers to output"
35-
STOP
36-
endif
37-
38-
! Will see if there are any lowercase, if so, print an error.
39-
if (lowers > 0) then
40-
write(*,'(g0.8)')"Usage: please input the count of fibonacci numbers to output"
41-
STOP
42-
endif
43-
44-
45-
! If all checks pass, we will enter this section. If non pass, we will not reach this point
46-
! Enter a loop
47-
read (argument, '(I10)') loop
48-
do i = 1, loop
49-
write(*,'(5g0)') i, ": ", currentnumber ! Print current iteration, and current number
50-
addednumber = previousnumber + currentnumber ! Replace the value of 'added number' with both 'current number' and 'previous number'
51-
previousnumber = currentnumber ! Replace the value of 'previous number' with 'current number'
52-
currentnumber = addednumber ! Replace the value of 'current number' with 'added number'
53-
end do
54-
55-
end program fibonacci
2+
implicit none
3+
integer(kind=8) :: prev, curr, next, n, i
4+
character(len=32) :: arg
5+
integer :: ios
6+
7+
if (command_argument_count() /= 1) then
8+
call usage()
9+
return
10+
endif
11+
12+
call get_command_argument(1, arg)
13+
arg = adjustl(trim(arg))
14+
if (len_trim(arg) == 0) then
15+
call usage()
16+
return
17+
endif
18+
19+
read(arg, *, iostat=ios) n
20+
if (ios /= 0 .or. n < 0) then
21+
call usage()
22+
return
23+
endif
24+
25+
if (n == 0) return
26+
27+
prev = 0
28+
curr = 1
29+
30+
do i = 1, n
31+
write(*,'(I0,": ",I0)') i, curr
32+
next = prev + curr
33+
prev = curr
34+
curr = next
35+
end do
36+
37+
contains
38+
39+
subroutine usage()
40+
write(*,'(A)') "Usage: please input the count of fibonacci numbers to output"
41+
end subroutine usage
42+
43+
end program fibonacci

0 commit comments

Comments
 (0)