ALevenson wrote:
> I just picked up Fortran yesterday at the suggestion of my mentor,
> which for me means I glance through a tutorial and try to get a
> program running that finds primes. I wrote this over the course of
> maybe 15 minutes, debugged it as I was able to over the next 20, and I
> don't think it is even close to compiling. Of course, I can't figure
> out the exact issues with it, so any help would be appreciated.
It would help a lot if you would also indicate which specific errors
you're having trouble resolving.
> program primes
>
> integer :: i, j, k, l, m, n, x
> real :: t
> logical :: notprime, even
>
> ! Program finds primes up to
> ! upper limit 'x' as defined by user
>
> write ( * , * ) 'Please input upper limit, x:'
> read ( * , * ) x
>
> do i = 2, x, 1
The 1 is superfluous here, though I don't think it's harmful.
> ! Form of FOR loop is
> ! do variable = start val, end val, step size
> if ( i == 2 ) then
> write ( * , * ) i
> end if
Logic problem: 2 is handled differently from everything else, but is
still included in the loop. Suggestion: Hoist this if statement outside
the loop (you'll need an additional check on x) and start the outer loop
from 3.
> even = .FALSE.
> notprime = .FALSE.
> j = i / 2
> t = i / 2
Logic problem: i / 2 is integer division. Context indicates that you
want floating point division. Suggestion: Change this line to read,
t = i / 2.0
This would not produce a compile-time error, but it will produce bad
output.
> if ( j == t ) then
> even = 1
Assigning 1 to a logical. Use .TRUE. instead.
> else
> l = SQRT( i )
> do m = 3, l, 2
> n = m / 2
> t = m / 2
> if ( n == t ) then
> notprime = 1
Assigning 1 to a logical. Use .TRUE. instead.
> end if
> end do
> end if
> if ( notprime == .FALSE. && even == .FALSE. ) then
You typed "&&", but meant ".AND.".
Using .EQ. with a logical. This is not allowed. You would have to use
..EQV., but the precedence of that operator is such that you would also
need additional parens for the logic to work as intended.
I would suggest a change in your logic, as well. You're looking for
"prime" and "odd", but test not not prime and not even. If you alter
the upstream parts accordingly, the if turns into...
IF (prime .AND. odd) THEN
which is much more readable.
> write ( * , * ) i
> end if
> end do
>
> stop
> end


|
34 Posts in Topic:
|
ALevenson <Andrew.Leve |
2008-07-24 10:55:18 |
|
ALevenson <Andrew.Leve |
2008-07-24 10:59:09 |
|
Craig Powers <craig.po |
2008-07-24 14:11:46 |
|
Paul van Delst <Paul.v |
2008-07-24 14:15:22 |
|
Dick Hendrickson <dick |
2008-07-24 18:06:28 |
|
Craig Powers <craig.po |
2008-07-24 14:09:35 |
|
ALevenson <Andrew.Leve |
2008-07-24 11:12:13 |
|
Craig Powers <craig.po |
2008-07-24 14:15:20 |
|
ALevenson <Andrew.Leve |
2008-07-24 11:19:35 |
|
Alois Steindl <Alois.S |
2008-07-24 20:28:47 |
|
Sebastian Hanigk <hani |
2008-07-24 20:45:13 |
|
ALevenson <Andrew.Leve |
2008-07-24 11:40:38 |
|
Gib Bogle <bogle@[EMAI |
2008-07-25 09:47:44 |
|
dpb <none@[EMAIL PROTE |
2008-07-24 17:43:53 |
|
"Kevin G. Rhoads&quo |
2008-07-25 20:29:05 |
|
dpb <none@[EMAIL PROTE |
2008-07-25 17:21:03 |
|
Richard L Walker <rlwa |
2008-07-25 19:06:03 |
|
dpb <none@[EMAIL PROTE |
2008-07-25 19:15:40 |
|
Ron Ford <ron@[EMAIL P |
2008-07-25 01:36:42 |
|
e p chandler <epc8@[EM |
2008-07-25 06:57:35 |
|
Ron Ford <ron@[EMAIL P |
2008-07-25 16:57:39 |
|
"Dr Ivan D. Reid&quo |
2008-07-25 23:12:58 |
|
e p chandler <epc8@[EM |
2008-07-25 17:14:15 |
|
nospam@[EMAIL PROTECTED]
|
2008-07-25 17:30:39 |
|
Ron Ford <ron@[EMAIL P |
2008-07-25 22:49:22 |
|
e p chandler <epc8@[EM |
2008-07-25 18:02:47 |
|
AnotherSquid <mai@[EMA |
2008-07-29 07:26:26 |
|
"Dr Ivan D. Reid&quo |
2008-07-29 19:07:40 |
|
Reinhold Bader <Bader@ |
2008-07-29 21:19:21 |
|
Craig Powers <craig.po |
2008-07-29 15:29:20 |
|
"Dr Ivan D. Reid&quo |
2008-07-29 22:51:25 |
|
Tobias Burnus <burnus@ |
2008-07-29 11:39:10 |
|
kargl@[EMAIL PROTECTED]
|
2008-07-29 19:29:07 |
|
AnotherSquid <mai@[EMA |
2008-07-30 06:49:43 |
|