cryptarithmes.py

Created by dkfe123

Created on September 24, 2025

1.45 KB

trouve les valeurs des lettres d’un cryptarithme au forçage. PEUT ETRE LENT ex : ab+a=ac. a est différent de 0 car c’est une première lettre d’un nombre. a,b et c sont des entiers de [0;10] différents. Ici, a=1;b=2 et c=3 Ou a=2;b=1 et c=3


from math import *
a=[]
r=[]
n1=int(input("nb1 "))
for i in [0]*n1:
  a.append(input("Mot "))
n2=int(input("nb2 "))
for i in [0]*n2:
  r.append(input("Resultat "))
l=[]
n=[]
s=[]
for i in range(len(a)):
  m=a[i]
  for k in range(len(m)):
    e=10**(len(m)-k-1)
    p=l.count(m[k])
    if p>0:
      p=l.index(m[k])
      n[p]+=e
      if k==0:
        s.append(m[k])
    else:
      l.append(m[k])
      n.append(e)
      if k==0:
        s.append(m[k])
for i in range(len(r)):
  m=r[i]
  for k in range(len(m)):
    e=0-10**(len(m)-k-1)
    p=l.count(m[k])
    if p>0:
      n[l.index(m[k])]+=e
      if k==0:
        s.append(m[k])
    else:
      l.append(m[k])
      n.append(e)
      if k==0:
        s.append(m[k])
def pos(v):
  for w in range(10):
    if v.count(w)>1:
      return False
  return True
def calc(v,n):
  re=0
  for w in range(len(v)):
    re=re+v[w]*n[w]
  if re==0:
    return True
  return False
def ajout(v,s,l):
  a=1
  for i in range(len(v)):
    if a==1:
      if v[i]==9:
        if s.count(l[i])>0:
          v[i]=1
        else:
          v[i]=0
      else:
        v[i]+=1
        a=0
  return v

v=[0]*len(l)
nt=0
va=[]
if len(l)<11:
  for i in range(len(v)):
    if s.count(l[i])>0:
      if v[i]==0:
        v[i]=1
  for z in range(10**len(l)-1):
    #print(v)
    nt+=1
    v=ajout(v,s,l)
    p=pos(v)
    if p:
      t=calc(v,n)
      if t:
        print("trouve")
        print(l)
        print(v)
        print(n)
        break

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.