Hi there,
I wrote a solution to the sieve exercise in Fortran.
I rather expected it to work , since I compiled a version of this on my own computer (together with a minimal main program which calles prime() for the argument values used in the test) which works fine.
However I keep getting "An error occurred while running your tests. " and I can’t figure out what’s going wrong.
Best Regards,
Eric
Here’s the code:
module sieve
implicit none
contains
function primes(limit) result(array)
integer, intent(in) :: limit
integer, allocatable :: array(:)
integer :: k
integer, allocatable:: numbers(:)
integer :: sqrt_limit
integer :: n = 2
if (limit > 1) then
allocate(numbers, source=[(k,k=1,limit)])
sqrt_limit = int(sqrt(real(limit)))
do while (n<= sqrt_limit)
do k=n*n,limit,n
numbers(k) = 0
end do
n = n+1
do while (n==0)
n= n+1
end do
end do
array = pack(numbers, numbers>1)
else
allocate(array(0))
end if
end function primes
end module sieve