The sum of integers between 1 and n is given by sigma^n _i=1 i = n(n+1)/2 Write
ID: 3791378 • Letter: T
Question
The sum of integers between 1 and n is given by sigma^n _i=1 i = n(n+1)/2 Write a Fortran program that prompts the user for n and reports the sum three ways using the algebraic formula above. using a counting loop using a do while loop (The filenames for the three parts should be hw05_01a. f95, hw05_01b. f 95, and hw05_01c.f95.) A common (but not efficient) Taylor series expansion lets us approximate the arctangent function using tan^-1 (x) = sigma^m _n=0 (-1)^n x^2n+1/2n+1 which has m+1 terms and is valid for x in [-1, 1]. The approximation becomes exact asm rightarrow infinityExplanation / Answer
hw05_01a.f95
//in this program summation is done based on formula...
PROGRAM summation
IMPLICIT NONE
INTEGER n
print*, 'ENTER THE NUMBER'
read*, n
print*, 'THE NUMBER IS', n
PRINT*, sum_n(n) ! etc
CONTAINS
RECURSIVE FUNCTION sum_n(N) RESULT(N_sum)
INTEGER, INTENT(IN) :: N
INTEGER :: N_sum ! also defines type of fact
IF (N /= 1) THEN
N_sum = N + sum_n(N-1)
ELSE
N_sum = 1
END IF
END function sum_n
END PROGRAM summation
hw05_01b.f95
//this program is used to find sum using do loop
program looping
implicit none
integer :: count, n, number,sum
print*, 'ENTER THE NUMBER'
read*, number
print*, 'THE NUMBER IS', number
n = number
sum = 0
do count=1,n
sum = sum + dble(count)
end do
write (*,*) ' Value of sum is ',sum
stop
end program looping
//program for inverse tan series using taylor series
program infiniteSeries
implicit none
integer :: n = 1
real :: degRad = 3.141592653589793 / 180
real :: atanApprox, atanIntrinsic = 0., mytan = 0., x = 0.
write(*,*)"Please enter a value in degrees"
read(*,*) x
x = x * degRad
atanIntrinsic = atan(x)
do n = 1, 10
mytan = atanApprox(x,n)
write(*,*) n, mytan
end do
write(*,*) "Intrinsic function atan() =", atanIntrinsic
end program infiniteSeries
real function atanApprox(x,n)
implicit none
real :: x, fact ! type of the function fact()
integer :: n, k, a
atanApprox = 0.
do k = 0, n
atanApprox = atanApprox + (-1)**k * x**(2*k+1) / fact(2*k+1)
end do
end function atanApprox
function fact(a)
implicit none
real :: fact
integer :: a, i
fact = 1.
do i = 1, a
fact = fact * i
end do
end function fact
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.