Help with simple Fortran program

In summary, The conversation discusses the need to create a program that prints the prime factorization of a number using trial division. The code provided uses a loop to divide the number by 2 and print the resulting factors. However, it only works for powers of 2 and does not continue for other numbers. The suggestion is to move the line "i=2" before the loop to ensure that the program continues to divide the number by 2 until it reaches 1.
  • #1
eku_girl83
89
0
I need to write a program that will print the prime factorization of a number.
Here's what I have so far:
integer:: n,i

print *, "enter integer"
read *, n
do while (n>1)
i=2
if (mod(n,i)==0) then
print *, i
n = n/i
else
i = i+1
end if
end do
end program

I'm just using trial division to find the prime factorization. For some reason, the program works for powers of 2 (4,8,16,32,64, etc.) but nothing else. It will factor a number such as 20 into (2,2) but doesn't go any farther. What is wrong with my program? Why does it stop trial division at i=2?
 
Technology news on Phys.org
  • #2
Change this:

do while (n>1)
i=2

I think that you need write:

i=2
do while (n>1)
 
  • #3


Your program looks like it has the right idea, but there are a few things that need to be fixed in order for it to work properly.

Firstly, you are only checking if the number is divisible by 2. This is why it works for powers of 2, but not for other numbers. You need to use a loop to check if the number is divisible by all integers from 2 up to n, not just 2.

Secondly, you are not updating the value of n when you find a factor. In your code, you are only dividing n by i, but you need to also update the value of n to be n/i. This will ensure that your loop continues until n is equal to 1.

Thirdly, you need to move the line "i=2" outside of the while loop. This will ensure that i is always starting at 2 for each iteration of the loop.

Finally, you should also add a check to make sure that i is not greater than n. This will prevent the loop from going on forever if the number entered is prime.

Here is a revised version of your code with these changes:

integer:: n,i

print *, "enter integer"
read *, n
i=2
do while (n>1)
if (mod(n,i)==0) then
print *, i
n = n/i
else
i = i+1
end if
if (i>n) then
print *, "The number entered is prime."
exit
end if
end do
end program

I hope this helps and good luck with your program!
 

1. What is Fortran and why is it used in scientific programming?

Fortran is a high-level programming language commonly used in scientific and engineering applications. It was specifically designed for numerical and scientific computation, making it a popular choice for researchers and scientists. Fortran's efficient handling of mathematical operations and ability to work with large datasets make it well-suited for scientific programming.

2. How can I get help with my simple Fortran program?

If you are new to Fortran or need assistance with your program, there are several resources available. You can consult online tutorials and documentation, join online forums or communities for Fortran users, or seek help from experienced programmers or computer science departments at universities.

3. What are some common errors or bugs in Fortran programs?

Some common errors in Fortran programs include syntax errors (such as missing semicolons or incorrect use of variables), logic errors (where the program runs but produces incorrect results), and runtime errors (such as division by zero or out-of-bounds array indexing). It is important to thoroughly test and debug your code to catch these errors and ensure the program runs correctly.

4. How can I improve the performance of my Fortran program?

To improve the performance of your Fortran program, you can consider optimizing your code by using efficient algorithms, minimizing the use of loops, and taking advantage of Fortran's built-in mathematical functions. Additionally, using parallel programming techniques and optimizing memory usage can also help to increase the program's speed and efficiency.

5. What is the best way to learn Fortran for scientific programming?

The best way to learn Fortran for scientific programming is to start with the basics, such as learning the syntax and structure of the language. From there, you can practice writing simple programs and gradually build up to more complex ones. Working on real-world projects and seeking guidance from experienced Fortran users can also help you improve your skills and understanding of the language.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
22
Views
595
  • Programming and Computer Science
Replies
12
Views
921
  • Programming and Computer Science
Replies
4
Views
463
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top