1. Write a program that finds the prime numbers in a given range using the sieve of Eratosthenes. program sieve ! find primes using the sieve of Eratosthenes implicit none integer, parameter :: n=100,nn=50 integer, dimension(n) :: numbers integer :: i, j, next ! initialize do i=1,n numbers(i)=i end do ! next will point to the next prime next=1 do ! find next prime do next=next+1 if (numbers(next)/=0) exit end do ! remove multiples of next numbers(2*next:n:next) = 0 if (next > nn) exit end do do i=1,n if (numbers(i) /=0) write(*,*) numbers(i) end do end program 2. Write a function that calculates cosine from its Taylor series and a main program that will utilize the function. Think how each term of the series can be calculated from the previous one using just a few floating-point operations. program cosine ! calculate cosine from its Taylor expansion implicit none integer :: n real :: x, cosx, abserr, relerr, truevalue write(*,*) 'give x in radians and nr of terms' read (*,*) x,n cosx = cosi(x, n) truevalue=cos(x) abserr = cosx - truevalue relerr = abserr/truevalue write(*,*) cosx, abserr, relerr contains real function cosi(x, n) real, intent(in) :: x integer, intent(in) :: n integer :: i, sig real :: term, sum, c sig = 1 term = 1 sum = 1 do i=2,n sig = -sig c = x**2 / (2.0*i-2) / (2.0*i-3) term = term * c sum = sum + sig * term end do cosi = sum end function cosi END PROGRAM cosine 3. Modify the harmonic series program so that the series is calculated by a function. The number of terms is given as a parameter. If an optional parameter giving the minimum value of terms is given the calculation is terminated also when this limit is reached. program harmonic ! find a partial sum of the harmonic seris implicit none real :: sum=0.0 integer (selected_int_kind(8)) :: i, n ! just to be sure we can handle a large number of terms write(6,*) "number of terms?" read(5,*) n sum = harm (n) write(6,*) n, sum sum = harm (n,0.01) write(6,*) n, sum contains real function harm (n, minval) ! calculate the sum of the harmonic series ! n is the number of terms ! exit if the next term is less than minval integer(selected_int_kind(8)), intent(in) :: n real, optional:: minval integer(selected_int_kind(8)) :: i real :: sum=0.0, term, minimum=10e-5 if (present(minval)) minimum=minval do i=1,n term=1.0/i if (term < minimum) exit sum = sum + 1.0/i end do harm = sum end function harm end program harmonic 4. Assume we have an array a(100). Write statements that: a) will find the sum of the elements whose index is even, b) set all negative elements to zero, c) replace all nonzero elements by their inverse. a) summa = sum(a(2:100:2)) b) where (a < 0.0) a=0.0 c) where (a /= 0.0) a=1/a