sharesuf.blogg.se

Give me a list of prime numbers to 100
Give me a list of prime numbers to 100








give me a list of prime numbers to 100 give me a list of prime numbers to 100

Otherwise I didn't edit much other then make the values get stored in and output array to be returned by the class. Thanks guys!!ĮDIT: Oh, and by the way, I didn't feel there was any need to import the math library for the square root of a value as the equivalent is just (n**.5). Notes: Solution 5 listed above (as proposed by user448810) turned out to be the fastest and honestly quiet creative and clever. Method 5 as described by user448810 (which I thought was quite clever): def primes_method5(n): Method 4 as described by Igor Chubin: def primes_method4(n): Method 3 as described by Igor Chubin: def primes_method3(n): If all(num % i != 0 for i in range(2, num)): Method 2 as described by Igor Chubin: def primes_method2(n): Method 1 as described by Igor Chubin: def primes_method1(n): So kudos to you, sir! In all examples I use a values of 1 million (1,000,000) as n. But I have to acknowledge for his clever solution, which turns out to be the fastest by far (of those I tested). Below are some modifications I did to create simple classes of examples by both and First off let me say it's all great information, thank you guys. I'm a proponent of not assuming the best solution and testing it. Need to check with even numbers, so 'i' value can be start with 3 and Like so: import mathĪs in the first loop odd numbers are selected, in the second loop no

give me a list of prime numbers to 100

You can improve it a little more by incrementing the range you check by 2, and thereby only checking odd numbers. If all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):įor small numbers like 101 it doesn't matter, but for 10**8 the difference will be really big. You can write the same much shorter and more pythonic: for num in range(2,101):Īs I've said already, it would be better to check divisors not from 2 to n-1, but from 2 to sqrt(n): import math If n is divisible by any of the numbers, it is not prime. You may see it by logging in.You need to check all numbers from 2 to n-1 (to sqrt(n) actually, but ok, let it be n). simple, effective, innovative.Ī reply falls below the community's threshold of quality. Moreso if such lists are easily available, e.g. When dealing with small primes (say up to 1,000,000 or even beyond), using precomputed lists maybe covenient. No need to praise it as "innovative", but also no need to mock it.Īfter all, the intend of the OP obviously dealt with small numbers and was not for example to show "a way to compute the 42th digit of the base 3 expansion of the 7th million prime number!"

give me a list of prime numbers to 100

Re^2: Easiest way to list prime numbers 1 to 100 (one-liner)Īs the claim was only to list primes below 100 easily, I found that solution actually quite O.K.










Give me a list of prime numbers to 100