Fortran How can I improve my Fortran program to factor numbers?

  • Thread starter Thread starter Jeepster
  • Start date Start date
  • Tags Tags
    Factoring Fortran
AI Thread Summary
The discussion revolves around a Fortran program designed to factor a number greater than one. The initial implementation successfully factors numbers but outputs each factor separately, while the desired output format is a single string representing the factorization, such as "2**3*3*5**2" for the input 600. To achieve this, a suggestion is made to introduce an inner loop that counts the occurrences of each factor and stores this count in a variable. The output should reflect single occurrences with a "*", while multiple occurrences should be formatted with "**" followed by the exponent. The user encounters an error related to the format statement in the second version of the program, specifically with the format specifier (I2, A2, I2). This indicates a need for correction in the format string to properly display the factors and their counts. The urgency of the request highlights the need for a quick resolution to meet a deadline.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
4
Views
2K
Replies
12
Views
2K
Replies
8
Views
2K
Replies
8
Views
4K
Replies
8
Views
4K
Replies
5
Views
5K
Back
Top