pyracube.py

Created by golem64

Created on December 12, 2020

364 Bytes

Script comprenant 2 fonctions permettant de : - calculer le nombre de cubes nécessaires pour construire n étages ; - calculer le nombre d’étages qui peut être construit avec k cubes. La disposition des cubes se fait en pyramide : 1 > 4 > 9 > 16 > 25 > etc…(Prenez des cubes et empilez les avec le nombre de cubes indiqués, d’abord 1 cube empilé sur 4 cubes, le tout empilé sur 9 cubes… N’oubliez pas de centrer le tout pour former une pyramide)


def etages(n):
  assert(n>0),"number must be higher than 0"
  cubes=[1]
  for n in range(1,n): cubes.append(cubes[n-1]+n*2+1)
  return sum(cubes)

def etage_max(k):
  assert(k>0),"number must be higher than 0"
  cubes=[1]
  while k>0:
    cubes.append(cubes[len(cubes)-1]+len(cubes)*2+1)
    k-=cubes[-1]
    if k<1: cubes.pop()
  return len(cubes)

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.