|
|
Up |
|
|
  |
Author: ALevensonALevenson Date: Jul 24, 2008 10:55
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.
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
|
| Show full article (1.35Kb) |
|
| | 33 Comments |
|
  |
Author: ALevensonALevenson Date: Jul 24, 2008 10:59
Erg, never mind that the algorithm I wrote doesn't even work. Here is
the (hopefully) fixed version.
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
|
| Show full article (1.16Kb) |
|
| | no comments |
|
  |
Author: Dick HendricksonDick Hendrickson Date: Jul 24, 2008 11:06
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.
Why do you think there are "issues". If you've tried to compile it,
fix the things you understand and try to compile again. Repeat
until you don't understand it and then show us what happened.
|
| Show full article (2.11Kb) |
| no comments |
|
  |
Author: Craig PowersCraig Powers Date: Jul 24, 2008 11:09
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.
|
| Show full article (2.70Kb) |
| no comments |
|
  |
Author: Craig PowersCraig Powers Date: Jul 24, 2008 11:11
ALevenson wrote:
> Erg, never mind that the algorithm I wrote doesn't even work. Here is
> the (hopefully) fixed version.
...
I missed something the last time around, I don't know if it was part
that you changed or not.
> n = i / m
> t = i / m
> ! n is an int, while t is a float
> ! so if n == t then m is a divisor of i
> ! meaning i is not prime
In this form, you need to write:
t = i / REAL(m)
or,
t = REAL(i) / m
(One of the two has to be REAL to avoid doing integer division.)
My other comments stand.
|
| |
| no comments |
|
  |
Author: ALevensonALevenson Date: Jul 24, 2008 11:12
>> prime specific hint.
>> write (*,*) 2
>> do i = 3 , x, 2
>> If you do that, you won't have to worry about the evenness
>> stuff, just test the odd numbers starting at 3.
Normally I do that, but I wanted to do some tests and see just how
fast Fortran can be compared to say, Perl.
>> j = i / 2
>> t = i / 2
>> if ( j == t ) then
>
> Why would you ever expect j and t to be different? Division
> is usually repeatable on most machines ;) .>
I'm probably wrong, but if i is 3, wouldn't j = 1, while t = 1.5? It
was my way of testing to see if it was an even divisor, because I
wasn't sure if Fortran had an Int() function.
Also, I getting the errors:
primes.f90:38.33:
|
| Show full article (1.14Kb) |
| no comments |
|
  |
Author: Craig PowersCraig Powers Date: Jul 24, 2008 11:15
ALevenson wrote:
>>> prime specific hint.
>>> write (*,*) 2
>>> do i = 3 , x, 2
>>> If you do that, you won't have to worry about the evenness
>>> stuff, just test the odd numbers starting at 3.
>
> Normally I do that, but I wanted to do some tests and see just how
> fast Fortran can be compared to say, Perl.
>
>>> j = i / 2
>>> t = i / 2
>>> if ( j == t ) then
>> Why would you ever expect j and t to be different? Division
>> is usually repeatable on most machines ;) .>
>
> I'm probably wrong, but if i is 3, wouldn't j = 1, while t = 1.5?
Where i = 3, 3 / 2 (integer division) = 1. Assigning to t then gives
you j = 1, t = 1.0.
|
| Show full article (1.67Kb) |
| no comments |
|
  |
Author: Paul van DelstPaul van Delst Date: Jul 24, 2008 11:15
ALevenson wrote:
> Erg, never mind that the algorithm I wrote doesn't even work. Here is
> the (hopefully) fixed version.
Apart from what Dick says.....
>
> program primes
>
> integer :: i, j, k, l, m, n, x
> real :: t
> logical :: notprime, even
So notprime is logical....
> notprime = .FALSE.
but later on you do
> notprime = 1
Don't do that. maybe
notprime = .true.
is what you want?
cheers,
paulv
|
| |
| no comments |
|
  |
Author: ALevensonALevenson Date: Jul 24, 2008 11:19
Paul, I noticed that and fixed it AFTER I reposted,but didn't think
it'd be worth posting it a third time for that fix alone, i'd just
pollute the board.
Dick, how do I correct that, exactly? Do I condense it to a single
ampersand?
Also,if == does not work, how do I compare logicals?
Thanks for all your help so far.
|
| |
| no comments |
|
  |
|
|
  |
Author: Alois SteindlAlois Steindl Date: Jul 24, 2008 11:28
ALevenson gmail.com> writes:
> Paul, I noticed that and fixed it AFTER I reposted,but didn't think
> it'd be worth posting it a third time for that fix alone, i'd just
> pollute the board.
>
> Dick, how do I correct that, exactly? Do I condense it to a single
> ampersand?
> Also,if == does not work, how do I compare logicals?
>
> Thanks for all your help so far.
Hello,
seems it is time to get a manual.
AND is .AND. and NOT is .NOT.
It is quite long-winded and artificial, to say
prime .eqv. .true.
instead of
prime
|
| Show full article (0.61Kb) |
| no comments |
|
RELATED THREADS |
  |
|
|
|
|
|