![]() ![]() ![]() Marking multiples is achieved by using list slicing in the form primeSum. In this case we will increase it from one million to two million to accommodate the original problem's limit.īasically, this is done using the principles of the sieve of Eratosthenes: find the next zero value in primeSum, set the prime sum, s, at that index and the next, and mark all the primes's multiples as composites (i.e., set to -1). The list would have to be big enough to handle all possible queries within the specified range. For example, primeSum = 17, the sum of all prime numbers not greater than 10. HackerRank poses a more demanding problem by having us find the sum of primes not greater than N (same as less than or equal to), where 1 ≤ N ≤ 10 6 for up to 10,000 queries.Ī reasonable way to approach solving this problem is to set up a list, primeSum, with the intention of building a one-to-one relationship with the query's value as the index and the sum of primes as the element. ![]() Using the function prime_sieve(N), introduced in Project Euler Problem 7, to find prime numbers less than N, we can solve this problem in less than 100ms using a couple lines of Python: from Euler import prime_sieve ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |