ilovepdf_merged
ilovepdf_merged
open(unit=44,file='matins.dat')
open(unit=55,file='outmat.txt')
read(44,*)((A(i,j),j=1,4),i=1,4)
write(55,*)"Answer of No (i)"
write(55,'(4I3)')((A(i,j),j=1,4),i=1,4)
write(55,'(/a)')"Answer of No (ii)"
write(55,'(16I3)')((A(i,j),j=1,4),i=1,4)
write(55,'(/a)')"Answer of No (iii)"
write(55,'(I3)')((A(i,j),j=1,4),i=1,4)
write(55,'(/a)')"Answer of No (iv)"
write(55,'(4I3)')(A(1,j),j=1,4)
write(55,'(I3)')(A(i,1),i=2,4)
write(55,'(/a)')"Answer of No (v)"
do i=1,4
do j=1,4
d3=0;
if(i==j)then
d3=d3+A(i,j)**3
end if
end do
end do
d2=0;
do i=1,4
d2=d2+A(i,5-i)
end do
write(55,*)"D3= ",d3
write(55,*)"D2= ",d2
If(d3>d2)write(55,*)"D3>D2"
write(55,'(/a)')"Answer of No (vi)"
write(55,'(4I3)')((A(i,j),i=1,4),j=1,4)
close(44)
close(55)
end
Input: matins.dat
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
Output: outmat.txt
Answer of No (i)
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
Answer of No (ii)
1 7 3 4 5 2 6 0 0 9 3 5 8 1 7 6
Answer of No (iii)
1
7
3
4
5
2
6
0
0
9
3
5
8
1
7
6
Answer of No (iv)
1 7 3 4
5
0
8
Answer of No (v)
D3= 216
D2= 27
D3>D2
Answer of No (vi)
1 5 0 8
7 2 9 1
3 6 3 7
4 0 5 6
allocate(arr(n))
do i=1,n
read(*,*)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
end program
Output: arrayout.txt
Ascendending Order:
23
34
43
64
68
Discendending Order:
68
64
43
34
23
open(unit=3,file='desout.txt')
do i=1,6
do j=1,6
call random_number (a(i,j))
a(i,j)=10+10*a(i,j)
end do
end do
open(unit=55,file='desoutput')
write(3,'(a/)')"F Descriptor."
write(3,'(6f20.8)')((a(i,j),j=1,6),i=1,6)
write(3,'(a/)')"E Descriptor."
write(3,'(6e20.7)')((a(i,j),j=1,6),i=1,6)
write(3,'(a/)')"Es Descriptor."
write(3,'(6es20.7)')((a(i,j),j=1,6),i=1,6)
end
Output: desout.txt
F Descriptor.
read(4,*)a,b
ans=g(a,b)
write(6,*)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
40
60
Output: gcd_out.txt
20
open(unit=34,file='PrimeOutput.txt')
write(34,'(t12,"n",15x,"Fn",6x,"Prime Test")')
write(34,'(t8,40("-"))')
do n=1,4
f=2**(2**n)+1
p=prime(f)
write(34,'(t11,i2,12x,i5,10x,l)')n,f,p
end do
end program
function prime(n)
implicit none
integer::i,n,prime
logical::p
do i=1,n/2
if(mod(n,i)==0)then
p=.false.
else
p=.true.
end if
end do
end function
Output: PrimeOutput.txt
n Fn Prime Test
----------------------------------------
1 5 T
2 17 T
3 257 T
4 65537 T
Answer to the question no. 6
Code: A1Q6.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)
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
34.0000 46.0000
78.0000 104.0000
T
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
Assignment-2
program bisection
implicit none
integer:: i=1
real::a=1,b=2,p0,p,f
real,parameter::tol=1e-8
open(unit=21,file='bisectout.txt')
p0=a
p=(a+b)/2.
if (f(a)*f(p)>0)then
a=p
end if
if (f(a)*f(p)<0)then
b=p
end if
p0=p
p=a+b/2.
i=i+1
end do
end
function f(x)
implicit none
real::x,f
f=x**3-x-1
end function
Output: bisectout.txt
Iteration No a b x f(x)
Relative Error
________________________________________________________________________________________
________________________________
1 1.000000 2.000000 1.500000 0.875000
0.333333
2 1.000000 1.500000 1.750000 2.609375
0.142857
3 1.000000 1.750000 1.875000 3.716797
0.066667
4 1.000000 1.875000 1.937500 4.335693
0.032258
5 1.000000 1.937500 1.968750 4.662079
0.015873
6 1.000000 1.968750 1.984375 4.829586
0.007874
7 1.000000 1.984375 1.992188 4.914428
0.003922
8 1.000000 1.992188 1.996094 4.957123
0.001957
9 1.000000 1.996094 1.998047 4.978539
0.000978
10 1.000000 1.998047 1.999023 4.989264
0.000489
11 1.000000 1.999023 1.999512 4.994630
0.000244
12 1.000000 1.999512 1.999756 4.997315
0.000122
13 1.000000 1.999756 1.999878 4.998657
0.000061
14 1.000000 1.999878 1.999939 4.999329
0.000031
15 1.000000 1.999939 1.999969 4.999664
0.000015
16 1.000000 1.999969 1.999985 4.999832
0.000008
17 1.000000 1.999985 1.999992 4.999916
0.000004
18 1.000000 1.999992 1.999996 4.999958
0.000002
19 1.000000 1.999996 1.999998 4.999979
0.000001
20 1.000000 1.999998 1.999999 4.999990
0.000000
21 1.000000 1.999999 2.000000 4.999995
0.000000
22 1.000000 2.000000 2.000000 4.999998
0.000000
23 1.000000 2.000000 2.000000 4.999999
0.000000
24 1.000000 2.000000 2.000000 5.000000
0.000000
25 1.000000 2.000000 2.000000 5.000000
0.000000
program fixed_point
implicit none
integer::i=1
real:: x,p(3)=[0.5,1.0,1.5],x1,f,g1,g1d,j,g2d,g2
real,parameter::tol=1e-6
open(unit=2,file='fixed_point_output.dat')
do j=1,3
x=p(j)
if(abs(g1d(x))<tol)then
write(2,8)
write(2,'(100("_"))')
do while(.true.)
x1=g1(x)
write(2,9)i,x,x1,g1(x1),abs(x1-x)
if(abs(x1-x)<tol)then
exit
end if
x=x1
i=i+1
end do
else
write(2,*)"Error"
end if
end do
write(2,'(///)')
do j=1,3
x=p(j)
if(abs(g2d(x))<tol)then
write(2,8)
write(2,'(100("_"))')
do while(.true.)
x1=g2(x)
write(2,9)i,x,x1,g1(x1),abs(x1-x)
if(abs(x1-x)<tol)then
exit
end if
x=x1
i=i+1
end do
else
write(2,*)"Error"
end if
end do
end
function g1(x)
implicit none
real::x,g1
g1=sqrt(x+10)/x
end function
function g1d(x)
implicit none
real::x,g1d
g1d=1/(2*x*sqrt(x+10))-sqrt(x+10)/x**2
end function
function g2(x)
implicit none
real::x,g2
g2=(x+10)**(1/4)
end function
function g2d(x)
implicit none
real::x,g2d
g2d=(1/4)*(x+10)**(-3/4)
end function
Output: fixed_point_output.dat
Error
Error
Error
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
1 0.500000 1.000000 3.316625 0.500000
2 1.000000 1.000000 3.316625 0.000000
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
2 1.000000 1.000000 3.316625 0.000000
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
2 1.500000 1.000000 3.316625 0.500000
3 1.000000 1.000000 3.316625 0.000000
program newton
implicit none
integer::i=1
real::x0=5,x,f,fd
real,parameter::tol=1e-5
open(unit=23,file='newtonoutput.txt')
7 format(3x, "Iteration
No",7x,"Pn-1",10x,"f'(Pn-1)",12x,"Pn",12x,"f(Pn)",12x,"Relative Error")
write(23,7)
write(23,'(100("_"))')
8 format(i9,t17,5f16.8)
do while(.true.)
x=x0-f(x0)/fd(x0)
write(23,8)i,x0,fd(x0),x,f(x),abs(x-x0)/x
if(abs(x-x0)<tol)then
exit
end if
x0=x
i=i+1
end do
end program
function f(y)
real::y,f
f=5*(y**2)+ cos(3*y)-2*exp(y)-exp(-y)
end function
function fd(y)
real::x,fd
fd=10*y-3*sin(3*y)-2*exp(y)+exp(-y)
end function
Output: newtonoutput.txt
program secant
implicit none
integer::i=1
real::x=0,x1=2.,x2,f
real,parameter::t=1e-6
open(unit=678,file='secant_out.txt')
do while(.true.)
x2=x1-((f(x1)*(x-x1))/(f(x)-f(x1)))
write(678,3)i,x,x1,x2,abs(x2-x1)
if(abs(x2-x1)<t )then
exit
end if
x=x1
x1=x2
i=i+1
end do
close(678)
end program
function f(y)
real::y
f=4.*y**3-1.-exp((x**2)/2.)
end function
Output: secant_out.txt
program false_position
implicit none
integer::i=1
real::a=1.,b=2.,c,f
real,parameter::t=1e-5
open(unit=789,file='falsi_out.txt')
do while(.true.)
c=b-((b-a)*f(b)/(f(b)-f(a)))
write(789,4)i,a,b,c,f(c),abs(c-b)
if(abs(c-b)<t .or. abs(f(c))<t)then
exit
end if
if(f(a)*f(c)<0)then
b=c
end if
if(f(a)*f(c)>0)then
a=c
end if
i=i+1
end do
close(789)
end program
function f(y)
real::y
f=exp(y)+2.**(-y)+(2.*cos(y))-6.
end function
Output: falsi_out.txt
iteration no a b c f(c)
abs error
________________________________________________________________________________________
____________
1 1.000000 2.000000 1.678308 -0.545674
0.321692
2 1.678308 2.000000 1.808103 -0.085739
0.191897
3 1.808103 2.000000 1.826538 -0.011645
0.173462
4 1.826538 2.000000 1.829006 -0.001549
0.170994
5 1.829006 2.000000 1.829334 -0.000205
0.170666
6 1.829334 2.000000 1.829377 -0.000027
0.170623
7 1.829377 2.000000 1.829383 -0.000003
0.170617
Assignment-3
a=(x-x0)/h
a1=(x1-xn)/h
open(unit=555,file='1i.txt')
open(unit=755,file='1o.txt')
read(555,*)(f(i),i=1,6)
do i=1,5
d(1,i)=f(i+1)-f(i)
end do
k=0
do i=2,5
do j=1,4-k
d(i,j)=d(i-1,j+1)-d(i-1,j)
end do
k=k+1
end do
term=f(1)
fact=1.
term1=f(6)
fact1=1.
do i=0,4
fact=fact*(a-i)/(i+1)
term=term+fact*d(i+1,1)
fact1=fact1*(a1+i)/(i+1)
term1=term1+fact1*d(i+1,5-i)
end do
close(555)
close(755)
end program
Input: 1i.txt
1.2
1.5
1.9
2.3
2.1
1.8
Output: 1o.txt
water level at 8:00 AM = 1.6633 meter
water level at 4:00 PM = 2.2883 meter
do i=1,n
read(10,*) x(i),y(i)
end do
read(10,*) xp
close(10)
! lagrange interpolation
ylag=0.0
do i=1,n
l=1.0
do j=1,n
if(j/=i) l=l*(xp - x(j))/(x(i) - x(j))
end do
ylag=ylag + l*y(i)
end do
ynewton=dd(1,1)
term=1.0
do i=1,n-1
term=term*(xp - x(i))
ynewton=ynewton + term*dd(1,i+1)
end do
Input: 2i.txt
1.0 52.0
2.0 5.0
4.0 -5.0
5.0 -40.0
7.0 10.0
3.0
Output: 2o.txt
interpolated value at x = 3.00
lagrange interpolation : 6.00000
newton divided difference : 6.00000
a1=(x1-xn)/h
open(unit=55,file='3i.txt')
open(unit=75,file='3o.txt')
read(55,*)(f(i),i=1,6)
do i=1,5
d(1,i)=f(i+1)-f(i)
end do
k=0
do i=2,5
do j=1,4-k
d(i,j)=d(i-1,j+1)-d(i-1,j)
end do
k=k+1
end do
term1=f(6)
fact1=1.
do i=0,4
fact1=fact1*(a1+i)/(i+1)
term1=term1+fact1*d(i+1,5-i)
end do
write(75,'("Sin(38) = ",f7.4)')term1
close(55)
close(75)
end program
Input: 3i.txt
0.2588190
0.3420201
0.4226183
0.5
0.5735764
0.6427876
Output: 3o.txt
Sin(38) = 0.6157
Assignment-4
Open(unit=41,file='CurrentOutput.txt')
exact=fd(t)
write(41,*)"The Exact Value of Current is:",exact
write(41,'(/)')
2 format(t37,"Approximation Value",6x,"Error")
write(41,2)
h=-.25
do i=0,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I1=(m(1)-n(1))/(2*h)
Write(41,*)"Using Three Point Midpoint Formula:",I1,abs(I1-exact)
h=.25
do i=0,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I2=(-3*m(0)+4*m(1)-m(2))/(2*h)
Write(41,*)"Using Three Point Endpoint Formula:",I2,abs(I2-exact)
h=.5
do i=1,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I3=(n(2)-8*n(1)+8*m(1)-m(2))/(12*h)
Write(41,*)"Using Five Point Midpoint Formula:",I3,abs(I3-exact)
h=.5
do i=0,4
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I4=(-25*m(0)+48*m(1)-36*m(2)+16*m(3)-3*m(4))/(12*h)
Write(41,*)"Using Five Point Endpoint Formula:",I4,abs(I4-exact)
end program
function f(t)
implicit none
real::f,t
f=60-60*exp(-t/5)
end function
function fd(t)
implicit none
real::fd,t
fd=12*exp(-t/5)
end function
Output: CurrentOutput.txt
The Exact Value of Current is: 4.41455364
Open(unit=5,file='RichardOutPut.txt')
exact=fd(x0)
Write(5,*)"The Exact Value is: ",exact
do i=0,4
n(i+1)=h/2**i
end do
do i=1,5
d(i,1)=(f(x0+n(i))-f(x0-n(i)))/(2*n(i))
end do
do j=2,5
do i=j,5
d(i,j)=(4**(j-1)*d(i,j-1)-d(i-1,j-1))/(4**(j-1)-1)
end do
end do
write(5,'(/)')
Write(5,'(t5,"O(h**2)",10x,"O(h**4)",10x,"O(h**6)",10x,"O(h**8)",10x,"O(h**10)")')
10 format(90('_'))
write(5,10)
Do i=1,5
Write(5,*)(d(i,j),j=1,i)
End Do
end program
function f(x)
real::x
f=x*exp(x)
end function
function fd(x)
real:: fd,x
fd=x*exp(x)+exp(x)
end function
Output: RichardOutPut.txt
The Exact Value is: 22.1671677
do i=1,7
read(10,*) t(i),x(i)
end do
! when t=0.0 s
v1=(x(2)-x(1))/(t(2)-t(1))
a1=(x(3)-2*x(2)+x(1))/((t(3)-t(2))*(t(2)-t(1)))
! when t=0.6 s
v2=(x(7)-x(6))/(t(7)-t(6))
a2=(x(7)-2*x(6)+x(5))/((t(7)-t(6))*(t(6)-t(5)))
write(12,'(t17,"velocity",8x,"acceleration")')
write(12,'(50("_"))')
write(12,*) "when t=0.0s:",v1,a1
write(12,*) "when t=0.6s:",v2,a2
close(10)
close(12)
end program motion
Input: 3input.txt
0.0 30.13
0.1 31.62
0.2 32.87
0.3 33.64
0.4 33.95
0.5 33.81
0.6 33.24
Output: 3output.txt
velocity acceleration
__________________________________________________
when t=0.0s: 14.9000168 -24.0003586
when t=0.6s: -5.69999552 -43.0000229
open(unit=4,file='integralOutput.txt')
do i=1,n-1
m(i)=f(a+i*h)
end do
I4=(3*h/10)*(f(a)+5*m(1)+m(2)+6*m(3)+m(4)+5*m(5)+f(b))
write(4,'(a,3f14.10)')"Using Weddles Formula the value is:
",I4,Abs(exact-I4),Abs(exact-I4)/exact
end program
function f(x)
implicit none
real::f,x
f=-1/((x-1)*(2+sqrt(2.)-2*x))
end function
function fd(x)
implicit none
real::fd,x
fd=-(1/sqrt(2.))*log(abs((x-1)/(sqrt(2.)-2*(x-1))))
end function
Output: integralOutput.txt
The exact Value is: 0.430767536
Aproximation Value Abs Error Relative
Error
Using Trapezoidal Rule the value is: 0.431104422 3.36885452E-04
7.82058574E-04
Using Simpson's 1/3 Rule the value is: 0.430768639 1.10268593E-06
2.55981672E-06
Using Simpson's 3/8 Rule the value is: 0.430769980 2.44379044E-06
5.67310735E-06
Using Weddles Formula the value is: 0.0475881808 0.3831793666 0.8895270228
a = 0.0
b = 1.0
tol = 1.0e-5
do i = 2,nmax
n = 2**(i-1)
h = (b - a) /n
s=0.0
do k = 1, n/2
s= s+f(a +(2*k-1)*h)
end do
do j = 2, i
R(i,j) = R(i,j-1) + (R(i,j-1) - R(i-1,j-1)) / (4.0**(j-1) - 1.0)
end do
write(8,*)( R(i,j),j = 1, i)
end program
Output: RombergOutPut.txt
0.774999976 0.783333302
0.782794118 0.785392165 0.785529435
0.784747124 0.785398126 0.785398543 0.785396457
0.785235405 0.785398185 0.785398185 0.785398185 0.785398185
Romberg integration result: 0.785398185
Estimated error: 1.72853470E-06
Assignment-5
Program mersenne
implicit none
integer(kind=8)::m(70),Mn,n
logical::prime
open(unit=5,file='mersenneoutput.txt')
write(5,'(t10,"n",29x,"Mn",6x,"Prime Test")')
write(5,'(t8,60("-"))')
do n=0,69
Mn=2**n-1
write(5,'(t10,i2,5x,i25,9x,l)')n,Mn,prime(Mn)
end do
end program
function prime(num)
implicit none
logical::prime
integer::num,i
if(num<2)then
prime=.false.
else
prime=.true.
end if
do i=2,int(num/i)
if(mod(num,i)==0)then
prime=.false.
else
prime=.true.
end if
end do
end function
Output: mersenneoutput.txt
n Mn Prime Test
------------------------------------------------------------
0 0 F
1 1 F
2 3 T
3 7 T
4 15 T
5 31 T
6 63 T
7 127 T
8 255 T
9 511 T
10 1023 T
11 2047 T
12 4095 T
13 8191 T
14 16383 T
15 32767 T
16 65535 T
17 131071 T
18 262143 T
19 524287 T
20 1048575 T
21 2097151 T
22 4194303 T
23 8388607 T
24 16777215 F
25 33554431 T
26 67108863 T
27 134217727 T
28 268435455 T
29 536870911 T
30 1073741823 T
31 2147483647 T
32 4294967295 F
33 8589934591 F
34 17179869183 F
35 34359738367 F
36 68719476735 F
37 137438953471 F
38 274877906943 F
39 549755813887 F
40 1099511627775 F
41 2199023255551 F
42 4398046511103 F
43 8796093022207 F
44 17592186044415 F
45 35184372088831 F
46 70368744177663 F
47 140737488355327 F
48 281474976710655 F
49 562949953421311 F
50 1125899906842623 F
51 2251799813685247 F
52 4503599627370495 F
53 9007199254740991 F
54 18014398509481983 F
55 36028797018963967 F
56 72057594037927935 F
57 144115188075855871 F
58 288230376151711743 F
59 576460752303423487 F
60 1152921504606846975 F
61 2305843009213693951 F
62 4611686018427387903 F
63 9223372036854775807 F
64 -1 F
65 -1 F
66 -1 F
67 -1 F
68 -1 F
69 -1 F
Program Hanoy_Tower
implicit none
integer::i
real(kind=16)::tol_sec,tol_days,Hn,Mn,n,disk(8),hanoi
open(unit=6,file='HanoyOutPut.txt')
disk=(/1,4,9,16,25,36,49,64/)
write(*,'(2x,"N",26x,"Hn(Moves)",30x,"Days",28x,"Mn")')
write(*,'(120("-")))')
do i=1,8
n=disk(i)
Hn=hanoi(n)
tol_sec=Hn*5
tol_days=tol_sec/(86400)
Mn=2.**n-1
write(*,'(f3.0,5x,F30.0,5x,f30.2,f30.2)')n,Hn,tol_days,Mn
end do
end program
if(n==1)then
moves=1
else
moves=2*hanoi(n-1)+1
end if
end function
function Mn(n)
implicit none
Real(kind=16)::Mn,n
if(n==0)then
Mn=0
else if(n==1)then
Mn=1
else
Mn=2**n-1
end if
end function
Output: HanoyOutPut.txt
N Hn(Moves) Days
Mn
----------------------------------------------------------------------------------------
--------------------------------
1. 1. 0.00
1.00
4. 15. 0.00
15.00
9. 511. 0.03
511.00
16. 65535. 3.79
65535.00
25. 33554431. 1941.81
33554431.00
36. 68719476735. 3976821.57
68719476735.00
49. 562949953421311. 32578122304.47
562949953421311.00
64. 18446744073709551615. 1067519911673006.46
18446744073709551615.00
program fibonacci
implicit none
integer,parameter::n=20
integer::i,fib,f(n),r,rfib
open(unit=7,file='FibonacciOutPut.txt')
r=fib(n,f)
r=rfib(n,f)
do i=1,n
write(7,*)f(i),f(i)
end do
end program
function fib(n,f)
implicit none
integer::i,n,fib,f(n)
f(1)=0
f(2)=1
do i=3,n
f(i)=f(i-2)+f(i-1)
end do
end function
else if(n==2)then
f(n)=1
r=f(n)
else
f(n)=rfib(n-2,f)+rfib(n-1,f)
r=f(n)
end if
end function
Output: FibonacciOutPut.txt
PROGRAM BitStringGenerator
IMPLICIT NONE
INTEGER, PARAMETER :: n = 8
INTEGER :: i,c
CHARACTER(len=n) :: bitString
OPEN(UNIT=10, FILE="4o.txt")
c = 0
WRITE(10,*) "Valid bit strings in ascending order:"
DO i = 0, 2**n - 1
WRITE(bitString, '(B8)') i ! Convert integer to binary string
IF (IsValid(bitString)) THEN
WRITE(10, *) TRIM(bitString)
c = c + 1
END IF
END DO
CONTAINS
FUNCTION IsValid(str) RESULT(valid)
CHARACTER(len=n), INTENT(IN) :: str
LOGICAL :: valid
valid = .NOT. (INDEX(str, '101') > 0 .OR. INDEX(str, '100') > 0)
END FUNCTION
END PROGRAM BitStringGenerator
Output: 4o.txt
program factorial
implicit none
integer:: n,r,fact,ncr,npr
open(unit=4,file='factorialoutputtttttttt.txt')
write(4,*)"10P6=",npr(10,6)
write(4,*)"10C4=",ncr(10,4)
write(4,*)"10C6=",ncr(10,6)
end program
function ncr(n,r)
implicit none
integer::n,r,ncr,ans,fact
ans=fact(n)/(fact(r)*fact(n-r))
end function
function npr(n,r)
implicit none
integer::n,r,npr,ans,fact
ans=fact(n)/(fact(n-r))
end function
Output: factorialoutputtttttttt.txt
10P6= 151200
10C4= 210
10C6= 210
Program tautology
implicit none
integer::i,j,k,exp1,m=.true.,exp2,n=.true.
logical::p,q,r
Open(unit=8,file="TAutologyOutput.txt")
write(8,'(3x,"p",5x,"q",5x,"r",5x,"(p^q)vr",5x,"(pvr)^(qvr)")')
write(8,'(50("-"))')
do i=0,1
do j=0,1
do k=0,1
p=(i==1)
q=(j==1)
r=(k==1)
if(.not.((p.and.q).or.r))then
exp1=.false.
end if
if(.not.((p.or.r).and.(q.or.r)))then
exp2=.false.
end if
write(8,'(3x,l,5x,l,5x,l,8x,l,12x,l)')p,q,r,(p.and.q).or.r,(p.or.r).and.(q.or.r)
end do
end do
end do
if(exp1==m)then
write(8,*)"(p^q)vr is a tautology."
else
write(8,*)"(p^q)vr is not a tautology."
end if
if(exp2==n)then
write(8,*)"(pvr)^(qvr) is a tautology."
else
write(8,*)"(pvr)^(qvr) is not a tautology."
end if
end program
Output: TAutologyOutput.txt
p q r (p^q)vr (pvr)^(qvr)
--------------------------------------------------
F F F F F
F F T T T
F T F F F
F T T T T
T F F F F
T F T T T
T T F T T
T T T T T
(p^q)vr is not a tautology.
(pvr)^(qvr) is not a tautology.
program rec_relation
implicit none
integer::n
real::a(50),a1(50)
open(unit=2,file='RecurrenceOutput.txt')
Write(2,'(t12,"n",4x,"Recurrenrence",4x,"Exact Value")')
Write(2,'(t10,40("-"))')
a(0)=1
a(1)=5
do n=2,50
a(n)=2*a(n-1)-a(n-2)+3
end do
do n=1,50
a1(n)=(3/2)*n**2+(5/2)*n+1
write(2,'(10x,i2,3x,f10.0,4x,f10.0)')n,a(n),a1(n)
end do
end program
Output: RecurrenceOutput.txt
open(unit=2,file='Jacobiinput.txt')
open(unit=4,file='JacobiOut.txt')
do i=1,n
read(2,*)(A(i,j),j=1,n)
end do
read(2,*)(b(i),i=1,n)
read(2,*)(x1(i),i=1,n)
close(2)
write(4,'("Iteration",10x,"x1",10x,"x2"10x,"x3",10x,"x4",10x,"x5",10x,"x6",10x,"Error")'
)
write(4,'(120("-"))')
do k=1,max_iter
do i=1,n
s=0.0
do j=1,n
if(i/=j) s=s+A(i,j)*x1(j)
end do
x2(i)=(b(i)-s)/A(i,i)
end do
er=maxval(abs(x2-x1))
write(4,'(i4,10x,6f11.5,6x,f12.5)')k,x2,er
end do
end program
Input: Jacobiinput.txt
4. -1. 0. -1. 0. 0.
-1. 4. -1. 0. -1. 0.
0. -1. 4. 0. 0. -1.
-1. 0. 0. 4. -1. 0.
0. -1. 0. -1. 4. -1.
0. 0. -1. 0. -1. 4.
0. 5. 0. 6. -2. 6.
0. 0. 0. 0. 0. 0.
Output: JacobiOut.txt
Iteration x1 x2 x3 x4 x5 x6
Error
----------------------------------------------------------------------------------------
--------------------------------
1 0.00000 1.25000 0.00000 1.50000 -0.50000 1.50000
1.50000
2 0.68750 1.12500 0.68750 1.37500 0.56250 1.37500
1.06250
3 0.62500 1.73438 0.62500 1.81250 0.46875 1.81250
0.60938
4 0.88672 1.67969 0.88672 1.77344 0.83984 1.77344
0.37109
5 0.86328 1.90332 0.86328 1.93164 0.80664 1.93164
0.22363
6 0.95874 1.88330 0.95874 1.91748 0.94165 1.91748
0.13501
7 0.95020 1.96478 0.95020 1.97510 0.92957 1.97510
0.08148
8 0.98497 1.95749 0.98497 1.96994 0.97874 1.96994
0.04918
9 0.98186 1.98717 0.98186 1.99093 0.97434 1.99093
0.02968
10 0.99452 1.98451 0.99452 1.98905 0.99226 1.98905
0.01791
11 0.99339 1.99533 0.99339 1.99670 0.99065 1.99670
0.01081
12 0.99801 1.99436 0.99801 1.99601 0.99718 1.99601
0.00653
13 0.99759 1.99830 0.99759 1.99880 0.99660 1.99880
0.00394
14 0.99927 1.99795 0.99927 1.99855 0.99897 1.99855
0.00238
15 0.99912 1.99938 0.99912 1.99956 0.99876 1.99956
0.00143
16 0.99974 1.99925 0.99974 1.99947 0.99963 1.99947
0.00087
do i=1,n
read(3,*)(A(i,j),j=1,n)
end do
read(3,*)(b(i),i=1,n)
read(3,*)(x1(i),i=1,n)
read(3,*)(exact(i),i=1,n)
close(3)
write(4,'("Iteration",7x,"x1",12x,"x2",15x,"x3",10x,"Error")')
write(4,'(70("-"))')
do iter=1,max_iter
do i=1,n
s=0.0
do j=1,n
if(i/=j)s=s+A(i,j)*x1(j)
end do
x2(i)=(b(i)-s)/A(i,i)
end do
er=maxval(abs(x2-exact))
write(4,'(i2,3f17.5,f10.5)')iter,x2,er
x1=x2
end do
end program
Input: jacobinputttttttttttttttttt.txt
2 -1 1
2 2 2
-1 -1 2
-1 4 -5
0 0 0
1 2 -1
Output: out2jacobi.txt
Iteration x1 x2 x3 Error
----------------------------------------------------------------------
1 -0.50000 2.00000 -2.50000 1.50000
2 1.75000 5.00000 -1.75000 3.00000
3 2.87500 2.00000 0.87500 1.87500
4 0.06250 -1.75000 -0.06250 3.75000
5 -1.34375 2.00000 -3.34375 2.34375
6 2.17188 6.68750 -2.17188 4.68750
7 3.92969 2.00000 1.92969 2.92969
8 -0.46484 -3.85938 0.46484 5.85938
9 -2.66211 2.00000 -4.66211 3.66211
10 2.83105 9.32422 -2.83105 7.32422
11 5.57764 2.00000 3.57764 4.57764
12 -1.28882 -7.15527 1.28882 9.15527
13 -4.72205 2.00000 -6.72205 5.72205
14 3.86102 13.44409 -3.86102 11.44409
15 8.15256 2.00000 6.15256 7.15256
16 -2.57628 -12.30511 2.57628 14.30511
17 -7.94070 2.00000 -9.94070 8.94070
18 5.47035 19.88139 -5.47035 17.88139
19 12.17587 2.00000 10.17587 11.17587
20 -4.58794 -20.35174 4.58794 22.35174
21 -12.96984 2.00000 -14.96984 13.96984
22 7.98492 29.93968 -7.98492 27.93968
23 18.46230 2.00000 16.46230 17.46230
24 -7.73115 -32.92460 7.73115 34.92460
25 -20.82787 2.00000 -22.82787 21.82787
do i = 1, n
read(24, *) (a(i,j), j = 1, n)
end do
read(24, *) (b(i), i = 1, n)
read(24, *) (x(i), i = 1, n)
write(23,'("itteration",6x,"x1",15x,"x2",15x,"x3"15x,"x4"15x,"x5"15x,"x6",/,104("_"))')
do k=1,max_i
x0=x
do i=1,n
ssum=0.0
do j=1,n
if(i<j) ssum=ssum+a(i,j)*x0(j)
if(i>j) ssum=ssum+a(i,j)*x(j)
end do
x(i)=(1-w)*x0(i)+(w)*(b(i)-ssum)/a(i,i)
end do
e=abs(x-x0)
write(23,'(5x,i0,6(6x,f10.6))')k,x
if(maxval(e)<t) exit
end do
write(23,'(//,"converged solution : ",6f10.6)')x
close(23)
close(24)
end program
Input: 3i.txt
4. -1. 0. 0. 0. 0.
-1. 4. -1. 0. 0. 0.
0. -1. 4. 0. 0. 0.
0. 0. 0. 4. -1. 0.
0. 0. 0. -1. 4. -1.
0. 0. 0. 0. -1. 4.
3. 2. 3. 3. 2. 3.
0. 0. 0. 0. 0. 0.
Output: 3o.txt
itteration x1 x2 x3 x4 x5
x6
________________________________________________________________________________________
________________
1 0.825000 0.776875 1.038641 0.825000 0.776875
1.038641
2 0.956141 1.020877 1.001877 0.956141 1.020877
1.001877
3 1.010127 1.001213 1.000146 1.010127 1.001213
1.000146
4 0.999321 0.999732 0.999912 0.999321 0.999732
0.999912
5 0.999994 1.000001 1.000009 0.999994 1.000001
1.000009
6 1.000001 1.000003 1.000000 1.000001 1.000003
1.000000
do j = 1, 3
read(43,*) u(:, j)
end do
write(32,'("the orthonormal vectors are :")')
do i=1,3
v(:,i)=u(:,i)
do j=1,i-1
v(:,i)=v(:,i)-(dot_product(u(:,i),v(:,j))/dot_product(v(:,j),v(:,j)))*v(:,j)
end do
v(:,i)= v(:,i)/sqrt(dot_product( v(:,i),v(:,i)))
write(32,'("w",i0,"=")')i
write(32,*), v(:,i)
end do
close(43)
close(32)
end program
Input: 4i.txt
1. 1. 1.
1. 1. 0.
1. 0. 0.
Output: 4o.txt
the orthonormal vectors are :
w1=
0.577350259 0.577350259 0.577350259
w2=
0.408248305 0.408248305 -0.816496611
w3=
0.707106769 -0.707106769 -1.40489496E-08
! gaussian elimination
do k=1,n-1
do i=k+1,n
b(i)=b(i)-a(i,k)/a(k,k)*b(k)
a(i,:)=a(i,:)-a(i,k)/a(k,k)*a(k,:)
end do
end do
! back-substitution
x(n)=b(n)/a(n,n)
do i=n-1,1,-1
x(i)=(b(i)-sum(a(i,i+1:n)*x(i+1:n)))/a(i,i)
end do
! initialize l and u
l=0.0
u=0.0
do i=1,n
l(i,i)=1.0
do j=i,n
u(i,j)=a(i,j)-sum(l(i,1:i-1)*u(1:i-1,j))
end do
do j=i+1,n
l(j,i)=(a(j,i)-sum(l(j,1:i-1)*u(1:i-1,i)))/u(i,i)
end do
end do
! solve ly=b
y(1)=b(1)
do i=2,n
y(i)=b(i)-sum(l(i,1:i-1)*y(1:i-1))
end do
! solve ux=y
x(n)=y(n)/u(n,n)
do i=n-1,1,-1
x(i)=(y(i)-sum(u(i,i+1:n)*x(i+1:n)))/u(i,i)
end do
Input: 5i.txt
2.0 -1.0 1.0
2.0 2.0 2.0
-1.0 -1.0 2.0
-1.0 4.0 -5.0
Output: 5o.txt
solution using gaussian elimination:
1.00000 2.00000 -1.00000
solution using lu decomposition:
1.00000 2.00000 -1.00000