benchmark.py

Created by cent20

Created on March 23, 2022

819 Bytes

Script de Xavier Andréani (tiplanet.org) pour tester les performances des calculatrices en python.


# https://tiplanet.org/forum/viewtopic.php?f=49&t=25151
# seuil(0.008) et isprimep(10000019)

from time import monotonic

def hastime():
  try:
    monotonic()
    return True
  except:
    return False

def seuil(d):
  timed,n=hastime(),0
  start,u=0 or timed and monotonic(),2.
  d=d**2
  while (u-1)**2>=d:
    u=1+1/((1-u)*(n+1))
    n=n+1
  return [(timed and monotonic() or 1)-start,n,u]

def nodivisorin(n,l):
  for k in l:
    if n//k*k==n:
      return False
  return True

def isprimep(n):
  t=hastime()
  s,l,k=0 or t and monotonic(),[3],7
  if n==2 or n==5:return True
  if int(n)!=n or n//2*2==n or n//5*5==5:
    return False
  if n<k:return n in l
  while k*k<n:
    if nodivisorin(k,l):l.append(k)
    k+=2+2*((k+2)//5*5==k+2)
  r=nodivisorin(n,l)
  return (t and monotonic() or 1)-s,r