#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 17 23:51:38 2025

@author: vincentleprince
"""

import numpy as np
import scipy.integrate as integr


def intRectangle(a,b,f,n):
    S=0
    h=float(b-a)/n
    for i in range(n):
        S+=f(a+i*h)
    return (b-a)*S/n

def intTrapeze(a,b,f,n):
    S=0
    h=float(b-a)/n
    val1=f(a)
    for i in range(n):
        val2=f(a+(i+1)*h)
        S+=0.5*(val1+val2)
        val1=val2
    return S/n   
    
##test 
def f(x):
    return 1./x
    
print(intRectangle(1,2,f,100))
print(intTrapeze(1,2,f,100))
print(np.log(2))

#avec scipy.integrate :

print(integr.quad(f,1,2))


# Monte Carlo
import random as rd

N = 1000
compteur = 0
for i in range(N):
    x , y = 1 + rd.random()  , rd.random()
    if y < 1/x:
        compteur += 1
freq = compteur/N
print(freq)

