Finding prime project euler

2024/9/22 3:52:08

Find the 10,001st prime number.

I am trying to do Project Euler problems without using copying and pasting code I don't understand. I wrote code that finds whether a number is prime or not and am trying to modify it to run through the first 10,001 primes. I thought that this would run through numbers until my break, but it's not working as intended. Bear in mind I have tried a few other ways to code this and this was the one I thought could work. I am guessing that I somewhat made a mess of what I had before.

import math
import itertools
counter = 0
counters = 0
for i in itertools.count():for n in range (2, math.ceil(i/2)+1):if i%n == 0:counter+=1if counter == 0 or i == 2:counters+=1if counters == 10001:breakelse:pass
print(i)
Answer

You are pretty close to the right solution. Remember next time to tell us what exactly you expected and what you got instead. Don't write something like

but it's not working as intended

Here are some changes you have to make to make it work and to make it faster !

import math
import itertools
counter = 0
counters = 0
nth_prime = 0
for i in itertools.count():# ignore numbers smaller than 2 because they are not primeif i < 2:continue# The most important change: if you don't reset your counter to zero, all but the first prime number will be counted as not prime because your counter is always greater than zerocounter = 0for n in range (2, math.ceil(i/2)+1):if i%n == 0:counter = 1# if you find out your number is not prime, break the loop to save calculation timebreak;# you don't need to check the two here because 2%2 = 0, thus counter = 1    if counter == 0:counters+=1else:passif counters == 10001:nth_prime = ibreak
print(nth_prime)

That code took me 36.1 seconds to find 104743 as the 10.001th prime number.

https://en.xdnf.cn/q/119655.html

Related Q&A

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…

file modifiaction and manipulation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…

how to save python session input and output [duplicate]

This question already has answers here:How to save a Python session, including input and output, as a text?(4 answers)Closed 2 years ago.All of the ways which discussed this question save the history …

flask_mysqldb Delete FROM variable table [duplicate]

This question already has answers here:Python sqlite3 parameterized drop table(1 answer)How do I use SQL parameters with python?(1 answer)Closed 6 years ago.So i use flask_mysqldb in a Flask(Python we…

Syntax Error at the end of a while loop

EDIT: This question was ask at the start of my learning process for python. The Syntax Error was produced by pythons IDLE with no trackback to speak of. This was the main cause of the problem and confu…

Creating an adjacency list class in Python

I was wondering how to create an adjacency list class Here is what I have so far:class AdjNode:def __init__(self, value):self.vertex = valueself.next = Noneclass Graph:def __init__(self):# Add edgesdef…

How can I separate a rust library and the pyo3 exported python extensions which wrap it

I have a rust library which provides useful functionality for use in other rust programs. Additionally I would like to provide this functionality as a python extension (using pyo3 and setuptools-rust, …

how do I count unique words of text files in specific directory with Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python convert path to dict

I have a list of paths that need to be converted to a dict ["/company/accounts/account1/accountId=11111","/company/accounts/account1/accountName=testacc","/company/accounts/acc…