suite.py

Created by edou559

Created on January 19, 2025

1.99 KB


def suite_arithmetique(un, r, n, terme_initial):
    """
    Calcule et affiche les détails d'une suite arithmétique.
    """
    print("\n--- Suite arithmétique ---")
    somme = 0
    for i in range(n):
        terme = un + i * r
        indice = terme_initial + i
        somme += terme
        print("U({}) = {} + {} * {} = {}".format(indice, un, i, r, terme))
    print("Somme des {} termes à partir de U({}) : {}".format(n, terme_initial, somme))


def suite_geometrique(un, q, n, terme_initial):
    """
    Calcule et affiche les détails d'une suite géométrique.
    """
    print("\n--- Suite géométrique ---")
    somme = 0
    for i in range(n):
        terme = un * (q ** i)
        indice = terme_initial + i
        somme += terme
        print("U({}) = {} * {}^{} = {}".format(indice, un, q, i, terme))
    if q != 1:
        somme_formula = un * (1 - q**n) / (1 - q)
        print("Somme (formule) des {} termes : {}".format(n, somme_formula))
    print("Somme (calculée) des {} termes : {}".format(n, somme))


def main():
    print("Choisissez le type de suite :")
    print("1. Suite arithmétique")
    print("2. Suite géométrique")
    choix = input("Votre choix (1 ou 2) : ")

    if choix == "1":
        un = float(input("Entrez le terme de départ (Un) : "))
        r = float(input("Entrez la raison (r) : "))
        terme_initial = int(input("Entrez l'indice du terme initial (par exemple 0, 1, 2) : "))
        n = int(input("Entrez le nombre de termes à calculer (n) : "))
        suite_arithmetique(un, r, n, terme_initial)
    elif choix == "2":
        un = float(input("Entrez le terme de départ (Un) : "))
        q = float(input("Entrez la raison (q) : "))
        terme_initial = int(input("Entrez l'indice du terme initial (par exemple 0, 1, 2) : "))
        n = int(input("Entrez le nombre de termes à calculer (n) : "))
        suite_geometrique(un, q, n, terme_initial)
    else:
        print("Choix invalide. Relancez le programme.")

# Lancer le programme
main()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.