Advertisement
Blogdemaths

Blogdemaths - Estimation entiers sans facteur carré

Dec 31st, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. #
  2. #  blogdemaths.wordpress.com - "En 2014, point de carré"
  3. #
  4.  
  5.  
  6. # Le programme suivant tire au hasard 100 nombres entre 1 et 1 000 000 et teste si chacun est sans facteur carré
  7. # puis renvoie un intervalle de confiance sur la proportion du nombre d'entiers sans facteur carré compris entre
  8. # 1 et 1 000 000.
  9.  
  10. import math
  11. import urllib.request
  12.  
  13.  
  14. def sans_facteur_carre(n):
  15.     depart=2
  16.     while n>1:
  17.         d=int(math.sqrt(n))
  18.         j=depart
  19.         while not n%j==0:
  20.             j+=1
  21.         if n%(j**2)==0:
  22.             return False
  23.         else:
  24.             n=n//j
  25.             depart=j+1
  26.     return True
  27.  
  28.  
  29. N=1000000
  30. taille_echantillon = 100
  31.  
  32.  
  33. siterandom = "http://www.random.org/integers/?num="+str(taille_echantillon)+"&min=1&max="+str(N)+"&col=1&base=10&format=plain&rnd=new"
  34. echantillon = urllib.request.urlopen(siterandom).read().decode('utf-8')
  35.  
  36. compteur=0
  37. for n in echantillon.split():
  38.     if sans_facteur_carre(int(n)):
  39.         compteur+=1
  40.    
  41.  
  42. frequence= compteur/taille_echantillon
  43. print("Sur un echantillon de ", taille_echantillon,"nombres pris au hasard entre 1 et "+str(N)+", il y a",round(frequence*100,3),"% des nombres qui sont sans facteur carré.")
  44.  
  45. confiance = 0.95
  46. t= 1.96
  47. mini= frequence - t * math.sqrt((frequence*(1-frequence))/taille_echantillon)
  48. maxi= frequence + t * math.sqrt((frequence*(1-frequence))/taille_echantillon)
  49.  
  50. print("Un intervalle de confiance au niveau de confiance de",confiance*100,"% est [",round(mini,3),";",round(maxi,3),"]")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement