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".
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top