Assignment-1
Q1: Matrix Operation
Code: mat.f90
program matrix
implicit none
integer::A(4,4),i,j,d3=0,d2
open(unit=393,file='mat_in.txt')
open(unit=939,file='mat_out.txt')
read(393,*)((A(i,j),j=1,4),i=1,4)
write(939,'(a,/)')"(i) the required matrix A:"
write(939,'(i7,3i3)')((A(i,j),j=1,4),i=1,4)
write(939,'(//,a,/)')"(ii) horizontal"
write(939,'(i7,15i3)')((A(i,j),j=1,4),i=1,4)
write(939,'(//,a,/)')"(iii) vertical"
write(939,'(i7)')((A(i,j),j=1,4),i=1,4)
write(939,'(//,a,/)')"(iv) 1st row and 1st column"
write(939,'(i7,3i3,/,3(i7,/))')(A(1,j),j=1,4),(A(i,1),i=2,4)
do i=1,4
do j=1,4
if (i==j)d3= d3 + A(i,j)**3
end do
end do
do i=1,4
d2=d2+A(i,5-i)**2
end do
write(939,'(a,i0,/,4x,a,i0)')"(v) d3=",d3,"d2=",d2
if (d3>d2) write(939,'(a)')"therefore d3>d2"
if (d3<d2) write(939,'(a)')"therefore d3<d2"
if (d3==d2) write(939,'(a)')"therefore d3=d2"
write(939,'(//,a,/)')"(vi) tranpose matrix:"
write(939,'(i7,3i3)')((A(j,i),j=1,4),i=1,4)
end program
Input: mat_in.txt
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
Output: mat_out.txt
(i) the required matrix A:
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
(ii) horizontal
1 7 3 4 5 2 6 0 0 9 3 5 8 1 7 6
(iii) vertical
1
7
3
4
5
2
6
0
0
9
3
5
8
1
7
6
(iv) 1st row and 1st column
1 7 3 4
5
0
8
(v) d3=252
d2=197
therefore d3>d2
(vi) tranpose matrix:
1 5 0 8
7 2 9 1
3 6 3 7
4 0 5 6
Q2: Array Operation
Code: as1_2.f90
program array
implicit none
integer::n,i,j,temp
integer,allocatable::arr(:)
open(unit=30,file='array_in.txt')
open(unit=32,file='array_out.txt')
read(*,*)n
if(n<5) then
write(*,*)"n must be at least 5"
stop
end if
allocate(arr(n))
do i=1,n
read(30,*)arr(i)
end do
do i=1,n-1
do j=1,n-1
if(arr(j)>arr(j+1)) then
temp=arr(j)
arr(j)=arr(j+1)
arr(j+1)=temp
end if
end do
end do
write(32,'("ascending order:")')
do i=1,n
write(32,*)arr(i)
end do
do i=1,n-1
do j=1,n-1
if(arr(j)<arr(j+1)) then
temp=arr(j)
arr(j)=arr(j+1)
arr(j+1)=temp
end if
end do
end do
write(32,'("descending order:")')
do i=1,n
write(32,*)arr(i)
end do
end program
Input: array_in.txt
1
1
2
3
5
8
5
34
2
4
6
8
56
42
Output: array_out.txt
ascending order:
1
1
2
3
5
5
8
34
descending order:
34
8
5
5
3
2
1
1
Q3: Function Design
Code: as1_3.f90
program descriptor
implicit none
integer::i,j
real::a(6,6)
do i=1,6
do j=1,6
call random_number(a(i,j))
a(i,j)=10.+a(i,j)*10.
end do
end do
open(unit=377,file='Fdes_out.txt')
write(377,'(a,/)')"F descriptor"
write(377,'(6f20.8)')((a(i,j),j=1,6),i=1,6)
write(377,'(///,a,/)')"E descriptor"
write(377,'(6e20.7)')((a(i,j),j=1,6),i=1,6)
write(377,'(///,a,/)')"ES descriptor"
write(377,'(6es20.7)')((a(i,j),j=1,6),i=1,6)
close(377)
end program
Input: Fdes_in.txt
[Missing file: Fdes_in.txt]
Output: Fdes_out.txt
F descriptor
13.61416531 18.69398117 11.76353168 16.12842178
18.91926575 10.24475574
15.05505562 14.28960609 12.59775162 15.32213020
10.25702095 11.47467422
11.10396481 13.68372345 19.26883507 19.72993088
10.51659584 11.30434799
14.31387711 12.03580856 15.05962944 17.89761925
19.23946762 18.57197952
16.29452896 13.43227100 15.46875763 17.34204483
17.64225769 19.81579971
16.69483757 10.67257118 10.42543507 15.91768837
12.70380020 13.87972927
E descriptor
0.1361417E+02 0.1869398E+02 0.1176353E+02 0.1612842E+02
0.1891927E+02 0.1024476E+02
0.1505506E+02 0.1428961E+02 0.1259775E+02 0.1532213E+02
0.1025702E+02 0.1147467E+02
0.1110396E+02 0.1368372E+02 0.1926884E+02 0.1972993E+02
0.1051660E+02 0.1130435E+02
0.1431388E+02 0.1203581E+02 0.1505963E+02 0.1789762E+02
0.1923947E+02 0.1857198E+02
0.1629453E+02 0.1343227E+02 0.1546876E+02 0.1734204E+02
0.1764226E+02 0.1981580E+02
0.1669484E+02 0.1067257E+02 0.1042544E+02 0.1591769E+02
0.1270380E+02 0.1387973E+02
ES descriptor
1.3614165E+01 1.8693981E+01 1.1763532E+01 1.6128422E+01
1.8919266E+01 1.0244756E+01
1.5055056E+01 1.4289606E+01 1.2597752E+01 1.5322130E+01
1.0257021E+01 1.1474674E+01
1.1103965E+01 1.3683723E+01 1.9268835E+01 1.9729931E+01
1.0516596E+01 1.1304348E+01
1.4313877E+01 1.2035809E+01 1.5059629E+01 1.7897619E+01
1.9239468E+01 1.8571980E+01
1.6294529E+01 1.3432271E+01 1.5468758E+01 1.7342045E+01
1.7642258E+01 1.9815800E+01
1.6694838E+01 1.0672571E+01 1.0425435E+01 1.5917688E+01
1.2703800E+01 1.3879729E+01
Q4: GCD Calculation
Code: as1_4.f90
program gcd
implicit none
integer::a,b,ans
open(unit=42,file='gcd_in.txt')
open(unit=62,file='gcd_out.txt')
read(42,*)a,b
ans=g(a,b)
write(62,*)ans
contains
recursive function g(x,y) result(gcd)
integer::x,y,gcd
if(mod(x,y)==0)then
gcd=y
else
gcd=g(y,mod(x,y))
end if
end function
end program
Input: gcd_in.txt
5
10
Output: gcd_out.txt
Q5: Fermat Number
Code: as1_5.f90
program Prime_Num
implicit none
integer::n,f
logical::p
open(unit=67,file='fermat_out.txt')
do n=1,4
f=(2**(2**n))+1
p=prime(f)
write(67,'(i20,5x,l)')f,p
end do
contains
function prime(x) result(p)
integer::x,i
logical::p
do i=2,x/2
if(mod(x,i)==0)then
p=.false.
exit
else
p=.true.
end if
end do
end function
end program
Output: fermat_out.txt
5 T
17 T
257 T
65537 T
Q6: Matrix Multiplication
Code: as1_6.f90
program matmultp
implicit none
integer::r1,c1,r2,c2,i,j,k
real,allocatable::a(:,:),b(:,:),c(:,:)
logical::v
read(*,*)r1,c1,r2,c2
open(unit=987,file='matmult_in.txt')
open(unit=977,file='matmult_out.txt')
allocate(a(1:r1, 1:c1), b(1:r2, 1:c2), c(1:r1, 1:c2))
read(987,*)((a(i,j),j=1,c1),i=1,r1)
read(987,*)((b(i,j),j=1,c2),i=1,r2)
!write(*,*)((a(i,j),j=1,c1),i=1,r1)
!write(*,*)((b(i,j),j=1,c2),i=1,r2)
call mat(a,r1,c1,b,r2,c2,c)
write(977,'(2(f15.4,2x))')((c(i,j),j=1,c2),i=1,r1)
v=g(a,r1,c1,b,r2,c2,c)
write(977,*)v
contains
function g(a,r1,c1,b,r2,c2,c) result(v)
implicit none
integer::i,j,r1,r2,c1,c2
real::a(1:r1,1:c1),b(1:r2,1:c2),c(1:r1, 1:c2),d(1:r1, 1:c2)
logical::v
d=matmul(a,b)
v=.true.
do i=1,r1
do j=1,c2
if(c(i,j)/=d(i,j))then
v=.false.
exit
end if
end do
end do
end function
end program
subroutine mat(a,r1,c1,b,r2,c2,c)
integer::r1,c1,r2,c2
real::a(1:r1,1:c1),b(1:r2,1:c2),c(1:r1, 1:c2)
c=0
do i=1,r1
do j=1,c2
do k=1,c1
c(i,j)=c(i,j)+a(i,k)*b(k,j)
end do
end do
end do
end subroutine
Input: matmult_in.txt
1. 2. 3. 4. 4. 6. 7.
5. 6. 7. 8. 5. 8. 3.
9. 10. 11. 12. 1. 4. 6.
13. 14. 15. 16. 5. 5. 7.
1. 5. 6. 7. 8. 5. 5.
3. 4. 5. 8. 5. 4. 2.
Output: matmult_out.txt
19.0000 22.0000
43.0000 50.0000
T
Q7: Amicable Number
Code: as1_7.f90
program Calc_Amicable_Num
implicit none
integer, parameter :: N=67000
integer :: i, calc_divisor_sum, divisorSum1, divisorSum2, prev
open(unit=78,file='amicable_out.txt')
do i=1, N
divisorSum1 = calc_divisor_sum(i)
divisorSum2 = calc_divisor_sum(divisorSum1)
if((divisorSum2 == i ).and. (i .ne. divisorSum1)) then
if(prev .ne. divisorSum1) then
write(78,*) i, divisorSum1
end if
prev = i
end if
end do
end program
function calc_divisor_sum(n)
implicit none
integer :: i, n, res, calc_divisor_sum
res=0
do i=1, nint(sqrt(real(n)))
if(mod(n, i) == 0) then
res = res + i
! As we are considering proper divisors
if(i == 1) then
cycle
end if
res = res + (n/i)
end if
end do
calc_divisor_sum = res
end function
Output: amicable_out.txt
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
63020 76084
66928 66992