Path: ...!news.mixmin.net!aioe.org!O+3FovYqd64puurqjy72dA.user.46.165.242.75.POSTED!not-for-mail From: Alain Ketterlin Newsgroups: fr.comp.lang.python Subject: Re: Autre exercice : calculer la somme de x chiffres. Date: Mon, 23 May 2022 12:56:36 +0200 Organization: =?utf-8?Q?Universit=C3=A9?= de Strasbourg Message-ID: <877d6czfdn.fsf@universite-de-strasbourg.fr.invalid> References: <87o7zod3s7.fsf@izac.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: gioia.aioe.org; logging-data="15969"; posting-host="O+3FovYqd64puurqjy72dA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) Cancel-Lock: sha1:vBT/8dgvalHW+C3XIkAabni+Cnw= X-Notice: Filtered by postfilter v. 0.9.2 Bytes: 1683 Lines: 23 Benoit Izac writes: > Le 22/05/2022 =C3=A0 17:11, Dominique a =C3=A9crit dans le message > =C2=A0: > >> Inutile de passer par une composition de liste : >> >> def addition(x): >> >> return sum(range(x+1)) >> >> print (addition(int(input('Fin de la sommielle ')))) >> >> Difficile sans doute d'=C3=AAtre plus concis... > > Mais on peut faire mieux en terme d'algorithme=C2=A0: > > def accumulate_sum_of(n): > return (n + 1) // 2 * (n + (n + 1) % 2) Hmm, pourquoi pas simplement n * (n+1) // 2 ? (Ou (n+1)*(n+2)//2 si on veut la somme jusqu'=C3=A0 n+1.) -- Alain.