Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > fr.comp.lang.python > #3974

[RESOLU] : Panne en Python...

Path csiph.com!pasdenom.info!usenet-fr.net!.POSTED!not-for-mail
From Olivier Miakinen <om+news@miakinen.net>
Newsgroups fr.comp.lang.python
Subject [RESOLU] : Panne en Python...
Date Sun, 2 Oct 2022 18:05:31 +0200
Organization There's no cabale
Lines 72
Message-ID <thccsb$1dd7$1@cabale.usenet-fr.net> (permalink)
References <th0qot$afg6$1@dont-email.me>
NNTP-Posting-Host 220.12.205.77.rev.sfr.net
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-15
Content-Transfer-Encoding 8bit
X-Trace cabale.usenet-fr.net 1664726731 46503 77.205.12.220 (2 Oct 2022 16:05:31 GMT)
X-Complaints-To abuse@usenet-fr.net
NNTP-Posting-Date Sun, 2 Oct 2022 16:05:31 +0000 (UTC)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.4
In-Reply-To <th0qot$afg6$1@dont-email.me>
Xref csiph.com fr.comp.lang.python:3974

Show key headers only | View raw


Le 28/09/2022 08:49, Dominique a écrit :
> 
> Il m'est demandé "d'arranger la liste en 15 nombres entiers de 1 à 15 de 
> telle sorte que la somme de 2 nombres voisins soit toujours un carré 
> parfait".


En partant de l'idée d'Alain Ketterlin, j'obtiens une solution plus courte
et plus élégante que celle donnée dans ton livre. En plus, elle permet de
trouver rapidement les solutions pour n'importe quel ensemble de nombres
de 1 à MAX, et pas seulement le cas où MAX = 15

=================================================================
import math

def est_un_carre(n):
    isqrt = math.isqrt(n)
    return isqrt * isqrt == n

def cherche(sequence, restant):
    if not restant:
        print(sequence)
    else:
        for x in restant:
            if est_un_carre(sequence[-1] + x):
                cherche(sequence + [x], restant - {x})

MAX_MAX=20

for MAX in range(1, MAX_MAX+1):
    print("MAX =", MAX)
    valeurs = {n for n in range(1,MAX+1)}
    for i in valeurs:
        cherche([i], valeurs - {i})
=================================================================

Résultat de l'exécution en 0,05 seconde pour MAX de 1 à 20 :

=================================================================
MAX = 1
[1]
MAX = 2
MAX = 3
MAX = 4
MAX = 5
MAX = 6
MAX = 7
MAX = 8
MAX = 9
MAX = 10
MAX = 11
MAX = 12
MAX = 13
MAX = 14
MAX = 15
[8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9]
[9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8]
MAX = 16
[8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9, 16]
[16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8]
MAX = 17
[16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17]
[17, 8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9, 16]
MAX = 18
MAX = 19
MAX = 20
=================================================================



-- 
Olivier Miakinen

Back to fr.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Panne en Python... Dominique <zzz@aol.com> - 2022-09-28 08:49 +0200
  Re: Panne en Python... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-09-28 16:11 +0200
  [NON RESOLU] : Panne en Python... AIEO <zzz@aol.com> - 2022-10-02 03:56 +0200
    Re: [NON RESOLU] : Panne en Python... Olivier Miakinen <om+news@miakinen.net> - 2022-10-02 13:03 +0200
    Re: [NON RESOLU] : Panne en Python... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-10-02 13:35 +0200
      Re: [NON RESOLU] : Panne en Python... AIEO <zzz@aol.com> - 2022-10-02 14:58 +0200
        Re: [NON RESOLU] : Panne en Python... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-10-02 15:58 +0200
          Re: [NON RESOLU] : Panne en Python... AIEO <zzz@aol.com> - 2022-10-02 16:32 +0200
            Re: [NON RESOLU] : Panne en Python... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-10-02 17:51 +0200
  [RESOLU] : Panne en Python... Olivier Miakinen <om+news@miakinen.net> - 2022-10-02 18:05 +0200
    Re: [RESOLU] : Panne en Python... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-10-02 21:22 +0200

csiph-web