خرید بک لینک

Vote count: 0

I wrote a quick sieve to test if a number is prime or not. I have two questions:

1) I tested a 200 digit prime, and it incorrectly says it wasn't prime. I believe this is down to floating point error (or something like that). How can I make this more accurate?

2) Is there a better way of writing this? I used decimal to deal with larger numbers. Is this the best way?

import math
from decimal import *
def isprime(n):
    i = 2
    a = 1
    if n == 1:
        retu 0
    if n == 2 or n == 3:
        retu 1
    while i < n**0.5 + 1:
        if Decimal(math.fmod(n,i)) == 0:
            a = 0
            i = n
        if Decimal(math.fmod(n,i)) != 0:
            i += 1
            a = 1
    retu a

asked 50 secs ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 18 فروردين 1395 ساعت: 7:24

صفحه بندی