How can I improve my Fortran program to factor numbers?

In summary, the conversation is about a program that factors a given number and outputs the factors in a specific format. The program works, but the teacher wants it to output the factors in a specific format. The solution is to use an inner loop that counts the number of times a factor is used and use "*" or "**" accordingly. The person requesting help has put together a code but is getting an error in the formatting section. They are seeking help as the assignment is due in a few hours.
  • #1
Jeepster
4
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
  • #2
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 "*".
 
  • #3
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
 
  • #4
I know I just signed up but this thing is due in a few hours.

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

What is factoring numbers in Fortran?

Factoring numbers in Fortran is the process of breaking down a given number into its prime factors. This is an important mathematical operation that is used in various applications, such as cryptography and data encryption.

Why is factoring numbers in Fortran important?

Factoring numbers in Fortran is important because it allows us to understand the structure of a number and find its divisors. This is particularly useful in cryptography, where prime numbers are used to ensure secure communication.

How does Fortran handle factoring numbers?

Fortran has built-in functions and subroutines that can be used to factor numbers. These include the FACTORIZATION and PRIME_FACTOR routines, which can be used to find the prime factors of a given number. Fortran also has a MODULO function that can be used to check if a number is a factor of another number.

Can Fortran handle large numbers for factoring?

Yes, Fortran has the capability to handle large numbers for factoring. The size of the number that can be factored depends on the specific implementation of Fortran and the hardware on which it is running. However, with modern computers and optimized algorithms, Fortran can handle very large numbers for factoring.

Are there any tips for improving the efficiency of factoring in Fortran?

Yes, there are some tips that can help improve the efficiency of factoring numbers in Fortran. These include using efficient algorithms, avoiding unnecessary loops and nested loops, and minimizing the number of function and subroutine calls. It is also important to optimize the code for the specific hardware on which it will be running.

Similar threads

  • Programming and Computer Science
Replies
4
Views
590
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
954
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
22
Views
731
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top