Fortran Help with simple Fortran program

  • Thread starter Thread starter eku_girl83
  • Start date Start date
  • Tags Tags
    Fortran Program
AI Thread Summary
The discussion centers on a program designed to print the prime factorization of a number using trial division. The current implementation successfully factors powers of 2 but fails for other numbers, only providing partial results. The issue arises because the variable 'i' is reset to 2 within the loop, preventing it from incrementing beyond 2 during subsequent iterations. To resolve this, the suggestion is to move the initialization of 'i' outside the loop, allowing it to increase properly and continue the trial division process for all potential factors. This adjustment is crucial for the program to function correctly for a wider range of integers.
eku_girl83
Messages
89
Reaction score
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
Change this:

do while (n>1)
i=2

I think that you need write:

i=2
do while (n>1)
 
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.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
4
Views
2K
Replies
12
Views
2K
Replies
19
Views
2K
Replies
11
Views
2K
Replies
19
Views
6K
Replies
13
Views
3K
Back
Top