Improve Project Euler problem 058 solution 1 (#4782)

* Fix typo

* Improve solution

* Retest

* Replace n with number
This commit is contained in:
Maxim Smolskiy 2021-10-25 11:07:10 +03:00 committed by GitHub
parent 5f7bb3e9f7
commit b55da04602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,11 +33,12 @@ So we check individually each one of these before incrementing our
count of current primes.
"""
from math import isqrt
def isprime(d: int) -> int:
def isprime(number: int) -> int:
"""
returns whether the given digit is prime or not
returns whether the given number is prime or not
>>> isprime(1)
0
>>> isprime(17)
@ -45,14 +46,15 @@ def isprime(d: int) -> int:
>>> isprime(10000)
0
"""
if d == 1:
if number == 1:
return 0
i = 2
while i * i <= d:
if d % i == 0:
if number % 2 == 0 and number > 2:
return 0
for i in range(3, isqrt(number) + 1, 2):
if number % i == 0:
return 0
i = i + 1
return 1