Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.06/5 (3 votes)
See more:
i = int(input())
j = 5 # fix the code (1) 
while (j <= (i/j)):
    if not(i%j): 
        print("not a prime")
        continue # fix the code (2)
    j = j + 2 # fix the code (3)
if (j > i/j): 
    print ("prime")


What I have tried:

Please help me to solve this problem
Posted
Updated 3-May-23 0:02am
Comments
Dave Kreskowiak 11-Aug-22 0:17am    
You never said what the problem was or any error messages.

The assignment you posted seems to be asking YOU to fix the problems with the code, the assignment being to understand what the algo is supposed to be doing, recognizing the problems, and coming up with the fixes.

We're not here to do your homework for you.
[no name] 11-Aug-22 5:00am    
Start by adding a prompt string to your input command, so the user knows what to do.
The first test you need is to check that i is an odd number, and reject it if it is not.
Then you need a loop that increments j from 3 to i/2 and increments by 2 each time round.
If i % j == 0 : it is not a prime so break out of the loop.
If you get to the end of the loop it is a prime.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
And that code isn't anything to do with "prime-ness"!

Think about what makes a number prime: is it divisible by itself and one, and no other number? So if X is divisible any number between 2 and X / 2 then it isn't prime - which isn't what your code is looking for at all! I'd suggest throwing that lot away, and starting over from scratch; it'll be easier than trying to fix it.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
How to solve this Python error code problem

You forgot to explain what is the problem with this code.
So first, this Python code is doing exactly what it is designed to do.

From my understanding, you try to find if an integer is prime or not. The problem is that you just invented some new mathematics for that purpose, since this new mathematics is not correct, result of code is not correct either.
The problem is that you need to rewrite code in a more classical way.

In order to understand what this code is doing, use the debugger.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
i = int(input())
j = 2
while (j <= (i/j)):
if not(i%j):
print("not a prime")
break
j = j + 1
if (j > i/j):
print ("prime")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900