How can I improve my Fortran program to factor numbers?

  • Context: Fortran 
  • Thread starter Thread starter Jeepster
  • Start date Start date
  • Tags Tags
    Factoring Fortran
Click For Summary

Discussion Overview

The discussion revolves around improving a Fortran program designed to factor numbers greater than one. Participants are focused on modifying the output format to display factors with their respective exponents, as required by the user's teacher.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares an initial Fortran program that successfully factors numbers but does not format the output as required.
  • Another participant suggests implementing an inner loop to count the occurrences of each factor and to format the output accordingly, using "*" for single occurrences and "**" for multiple occurrences.
  • A later reply indicates that the user attempted to modify the program but encountered an error related to the format statement in Fortran.
  • Participants discuss the specific error message regarding the format specifier, indicating a potential issue with the syntax used in the format statement.

Areas of Agreement / Disagreement

Participants generally agree that the program needs modification to meet the output requirements, but there is no consensus on the specific implementation details or how to resolve the encountered error.

Contextual Notes

The discussion includes unresolved issues regarding the correct syntax for format statements in Fortran and the logic needed to count factors and format them appropriately.

Who May Find This Useful

Individuals interested in programming in Fortran, particularly those working on number theory or factorization algorithms, may find this discussion relevant.

Jeepster
Messages
4
Reaction score
0
Got to write a program that factors a given number(>1)

Here is what I have:

program Factors
implicit none
real::n, i

print*, "enter a number:"
read*, n

i=2
do while (n>1)
if (mod(n, i)==0) then
i=i+1
n=n/i
else
write (*,*) ' X= ', X
end if
end do


end program

It works great but let's say you put in 600, it will give

2
2
2
3
5
5

which is right but the teacher wants it to say:

2**3*3*5**2

I don't have a clue how to make it do that.

Thanks for any help. :)
 
Technology news on Phys.org
You'll need an inner loop that counts how many times a given factor is used, storing that in another variable. If it's used once write it with a "*", otherwise write it with a "**", the exponent, then another "*".
 
ok. I put this together but I am getting an error


program Factors
implicit none
real::n, i, x
integer::f, c=20

print*, "enter a positive integer:"
read*, n


i=2
do while (n>1)
if (mod(n, i)==0) then
i=i+1
n=n/i
else


write(*, 100) i, "**", c
100 format(I2, A2, I2, $)
end if
end do


end program
 
I know I just signed up but this thing is due in a few hours.

Any help is much appreciated.
 
well what's the error?
 
It says there is an error in the (I2, A2, I2, $). It has an arrow pointing to the first I2.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K