Range prime number

A forum for general discussion of the Python programming language.

Range prime number

Postby doctterdre » Tue Sep 27, 2016 4:56 pm

So basically I have to write a program that take the formula i**2 + i + 41 and the range 0-100 and determines if the output is prime or not. the output should look like this:

41 is prime number
43 is a prime number
47 is a prime number
.
.
.
1681 is not a prime number
.
.
.
etc.

my code is only showing the last number in the range, 100. It shows 10141 is a prime number. How do I make it show all numbers? Thanks

Code: Select all
for i in range(101):
    
i**41
if (i**41 i) == 0:
    print(
i**41,"is not a prime number")
else:
    print(
i**41,"is a prime number"
Last edited by micseydel on Tue Sep 27, 2016 5:04 pm, edited 1 time in total.
Reason: Code tags. Initial post lock.
doctterdre
 
Posts: 8
Joined: Thu Sep 22, 2016 9:44 pm

Re: Range prime number

Postby Yoriz » Tue Sep 27, 2016 6:03 pm

The if statement onwards is not indented inside of the for loop.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Range prime number

Postby doctterdre » Tue Sep 27, 2016 11:28 pm

How would I fix it? Would I just indent everything? Thanks
doctterdre
 
Posts: 8
Joined: Thu Sep 22, 2016 9:44 pm

Re: Range prime number

Postby Ofnuts » Wed Sep 28, 2016 7:14 pm

Code: Select all
for i in range(101):
    i**2 + i + 41
    if (i**2 + i + 41 % i) == 0:
        print(i**2 + i + 41,"is not a prime number")
    else:
        print(i**2 + i + 41,"is a prime number")

Note that one statement above does nothing... But then you do the same computation 4 times (in the code, only three times in any execution). You may want to investigate using a variable.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests

cron