I'm a physicist, and I've been doing work with Fortran a lot recently. Originally i used Java extensively for recreation because it was the first language I learned, but I've abandoned it for Fortran and C++. I have an amateur passion for prime numbers and so I created a prime number sieve. I was able to find all the prime numbers up to 2^31 in 15 seconds. This is the maximum array size for Java so that was the end of that. I ported the code carefully( I mean CAREFULLY, I was so upset my code was slow and that I couldn't find an error that I ported my Fortran code back into Java to verify that it wasn't my fault and then ported it back to Fortran, erasing each iteration!). The problem is that around 800,000,000 Fortran will grind to a halt. Up to that point its beating Java, but after that it's insanely slow. I spent a few hours graphing it and fitting a curve. It slows exponentially and could take hundreds of years to solve up to Javas level. I've asked many people to no avail. Why is this happening to me?!?! Are there any wise Fortran coders who can help me? I'm running a Macbook Pro late 2013 i5. My code is below.
program sieve
integer(4),allocatable:: prime(:)
integer(4)::a,max,b,primeCount
write(*,*)"Welcome to the slow prime number sieve!"
write(*,*)"--------------------------------------------"
write(*,*)"Up to what numbers do you need to find primes for?"
write(*,*)"Enter a number below 2^(32-1)"
read*, max
primeCount=0
allocate(prime(max))
prime(1)=1
do a=2,int(sqrt(real(max))) !the main loop
if(prime(a)==0)then !if the number is marked as prime procede
do b=2*a,max,a !eliminate all the numbers that are multiples of the number
if(prime(b)==0)then !but only spend time eliminating if the number is still marked prime
prime(b)=1
end if
end do
end if
end do
do a=1,max
if(prime(a)==0)then
primeCount=primeCount+1
end if
end do
print*, primeCount
end program
EDIT: My machine has 4 Gigs of ram installed