Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Last active December 26, 2015 19:39
Show Gist options
  • Select an option

  • Save adrientetar/7202742 to your computer and use it in GitHub Desktop.

Select an option

Save adrientetar/7202742 to your computer and use it in GitHub Desktop.
TP d'informatique n°3, PCSI 2013-2014 — Lycée Carnot, Paris.
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #1
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
def somme(n):
res = 0
for i in range(1,n+1):
res += i
return res
def factorielle(n):
res = 1
for i in range(2,n+1):
res *= i
return res
n = int(input("Donner un entier naturel n:"))
print("La somme des entiers de 1 à n est " + str(somme(n)) + ".")
print("n! = " + str(factorielle(n)) + ".")
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #2
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-2)+fib(n-1)
# That one's far faster
def fib_increment(n):
a,b = 0,1
for _ in range(n):
a,b = b,a+b
return a
for i in range(100):
print(fib_increment(i))
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #3
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
def syracuse(n):
while True:
print(n)
if n == 1:
break
elif n % 2 == 0:
n /= 2
else:
n = 3*n+1
syracuse(2011)
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #4
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
def encadrer(n):
cpt = 0
while abs(n) >= 10:
n /= 10
cpt += 1
if n <= 0:
return -cpt-1 # we print n and n+1
return cpt
n = encadrer(int(input("Donner un entier s:")))
print("s est compris entre 10^" + str(n) + " et 10^" + str(n+1) + " inclus.")
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #5
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
import random
def demander():
return int(input("Quel est le nombre secret?"))
def nombreSecret(n):
cur = demander()
while cur != n:
if cur > n:
print("Trop grand!")
else:
print("Trop petit!")
cur = demander()
print("C'est ça!")
print("L'ordinateur choisit un nombre...")
nombreSecret(random.randint(0, 99))
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #6
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
def demander(joueur):
return int(input("Joueur " + str(joueur) + ", combien en prenez-vous?"))
def jouer(joueur, n):
print("Il y a " + str(n) + " bâtons.")
input = demander(joueur)
while input > n:
print("Vous ne pouvez pas en prendre " + str(input) + "!")
input = demander(joueur)
return input
def nim(n):
chg_joueur = True
while n > 0:
chg_joueur = not(chg_joueur)
n -= jouer(1+int(chg_joueur), n)
print("Le joueur " + str(1+int(not(chg_joueur))) + " remporte la partie!")
n = int(input("Combien y a t-il de bâtons?"))
if n > 0:
nim(n)
#!/usr/local/bin/python3
#
# PCSI, TP d'informatique n°3. #7
# Copyright (C) 2013, Adrien Tétar. All Rights Reserved.
#
import random
def monteCarlo(i):
cercle,total = 0,0
for _ in range(i):
x,y = random.random(),random.random()
if x**2+y**2 <= 1:
cercle += 1
total += 1
n = cercle / total
print("Dans le cercle: " + str(cercle) + ", total: " + str(total) + ".")
print("Le ratio de Monte-Carlo est de " + str(n) + ".")
print("pi ~ " + str(4*n) + ".")
monteCarlo(100000)

TP d'informatique n°3

  1. Somme et produit des entiers de 1 à n
  2. Suite de Fibonacci
  3. Suite de Syracuse
  4. Détermination d'un ordre de grandeur (sans la fonction log)
  5. Jeu du nombre secret
  6. Jeu de Nim (2 joueurs, chacun enlève entre 1 et 4 bâtons, celui qui prend le dernier a perdu)
  7. Approximation de pi par la méthode de Monte-Carlo (tirage aléatoire de nombres dans le carré unité et obtention du ratio du nombre de points dans le cercle de rayon 1 sur le nombre total de points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment