cb_lin_v001.py

Created by polettielio

Created on April 06, 2023

1.92 KB

Réciprocité de “a(x)|b(x) ∀x∈ℤ” (programme de maths expertes) :

Pour deux fonctions a(x) et b(x) et une série de solutions x possibles données, calculer les solutions a(x)=y et b(x)=z pour chaque x et indiquer si y|z.

Renvoie une liste de solutions x vérifiant a|b et, dans le cas échéant, une liste des x indiqués ne vérifiant pas a|b.

Utilisation : exécuter cb(), définir les fonctions a(x) et b(x) avec la syntaxe python (ex. 2*x+3), puis entrer chaque valeur possible de x à tester. L’aide est accessible avec la commande .. une fois le prompt affiché. Les fonctions a(x) et b(x) sont évaluées avec eval(a), il faut que leur contenu soit une expression cohérente en python qui retourne un nombre.

IMPORTANT : l’émulateur Numworks en ligne plante lors de eval(a) et eval(b) malgré a et b expressions correctes alors qu’il ne devrait pas, ce script est tout à fait correct et marche sans problème sur calculatrice réelle et dans tout autre interpréteur python.


def cb(a=False,b=False,c=False):
  if a==False or b==False or c==False:
    a=input("a: ")
    b=input("b: ")
    c=[]
    n=0
    print("CBLIN: help = ..")
    while True:
      t=input("#"+str(n)+" ")
      if t=="**":
        break
      elif t=="//":
        return
      elif t=="..":
        print("CBLIN cmd help:")
        print(" ..   show this help")
        print(" **   begin calculation")
        print(" //   exit cblin")
        print(" --   show list, a, b")
        print(" ++   detail list, a, b")
        print(" ((   delete x from list")
        print(" ))   delete entire list")
        print(" a    set a expression")
        print(" b    set b expression")
      elif t=="++":
        print("a=",a)
        print("b=",b)
        for i in range(len(c)):
          print("c"+str(i),c[i])
      elif t=="--":
        print("a=",a," ::  b=",b)
        print("c=",c)
      elif t=="))":
        print("delete entire list?")
        h=input("0=no, 1=yes> ")
        if h==1:
          c=[]
      elif t=="((":
        print("index to delete:")
        h=input("> ")
        try:
          print("del #"+str(h),":",c[int(h)])
          del c[int(h)]
        except IndexError:
          print("err/out of range")
        except:
          print("err/not int",h)
      elif t=="a":
        print("sat a, cancel = ..")
        h=input("> ")
        if h!="..":
          a=h
      elif t=="b":
        print("set b, cancel = ..")
        h=input("> ")
        if h!="..":
          b=h
      else:
        try:
          c.append(int(t))
          n+=1
        except:
          print("err/not int")
    print("====")

  print("n, a(x)=y, b(x)=z, y|z")
  o=[]
  p=[]
  for x in c:
    y=eval(a)
    z=eval(b)
    if z%y==0:
      w=str(y)+"|"+str(z)
      o.append(x)
    else:
      w="!"+str(z%y)
      p.append(x)
    print("n={}, {}={}, {}={}, {}".format(x,a,y,b,z,w))
  print()
  if len(p)>0:
    print("bad:",p)
  print("s=",o)

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.