MHB How does nested for loop works, can you explain with dry run?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Explain Loop Works
AI Thread Summary
The discussion centers on a Python code snippet intended to print prime numbers up to a specified integer n. The original code fails to include the number 2 in its output due to an indentation issue with the else statement. The proposed solution suggests that the else statement should be aligned with the inner for loop, ensuring that numbers are printed only when they are determined to be prime. The dry run of the modified code reveals that it incorrectly prints non-prime numbers, such as 4 and 6, due to the placement of the print statement. The analysis highlights the need for proper control flow to accurately identify and print only prime numbers within the specified range.
shivajikobardan
Messages
637
Reaction score
54
Here is the code that I am talking about-:
Code:
n=int(input("Enter a number"))
for num in range(2,n+1):
    for i in range(2,num):
        if(num%i==0):
            break
    else:
        print(num,end="")

If I give n=5 output should be 2,3,5.

Here is my dry run. Everything is fine except for 2 where I am not getting 2 as output. What has gone wrong here? I don't understand what is gone wrong here?
1-min.jpg
 
Technology news on Phys.org
Your else statement is not properly indented to align with the if statement. I believe it should be like this.

Code:
n=int(input("Enter a number"))
for num in range(2,n+1):
    for i in range(2,num):
        if(num%i==0):
            break
        else:
            print(num,end="")
 
If n= 5 then the "outer loop" runs for num= 2 to num= 6.

For num= 2, the "inner loop" runs for i= 2 to i= 2 (in other words, i is only 2). 2 divided by 2 is 1 with no remainder so 2%2= 0 so "break" and go on to

For num= 3, the "inner loop runs for i= 2 to i= 3. 3 divided by 2 is 1 with remainder 1 so 3%2= 1 so print 3. 3 divided by 3 is 1 with no remainder so 3%3=0 so "break" and go on to

For num= 4, the inner loop runs for i= 2 to 4. 4 divided by 2 is 2 with no remainder so 2%2= 0 "break". 4 divided by 3 is 1 with remainder 1 so print 4. 4 divided by 4 is 1 with remainder 0 so 4%4= 0 "break"

For num= 5, the inner loop runs for i= 2 to 5. 5 divided by 2 is 2 with remainder 1 so 5%2= 1 so print 5. 5 divided by 3 is 1 with remainder 2 so 5%3= 2 so print 5. 5 divided by 4 is 1 with remainder 1 so 5%4= 1 so print 5. 5 divided by 5 is 1 with remainder 0 so break.

For num= 6, the inner loop runs for i= 2 to 6. 6 divided by 2 is 3 with no remainder so 6%2= 0 so break. 6 divided by 3 is 2 with no remainder so 6%3= 0 so break. 6 divided by 4 is 1 with remainder 2 so 6%4= 2 so print 6. 6 divided by 5 is 1 with remainder 1 so 6%5= 1 so print 6. 6 divided by 6 is 1 with no remainder so 6%6= 0 so break.

The result will be to print "3 4 5 5 5 6 6".
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top