Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Created October 28, 2012 20:01
Show Gist options
  • Save adrientetar/3969687 to your computer and use it in GitHub Desktop.
Save adrientetar/3969687 to your computer and use it in GitHub Desktop.
Nombres cubiques (v1.0)
/**
* Ce programme calcule les entiers cubiques inférieurs
* à un entier donné.
* @author Adrien Tétar
* @version 1.0
*
* Copyright © 2012, Adrien Tétar. All rights reserved.
*/
import javax.swing.JOptionPane;
public class Nombres_cubiques
{
/* Display a value of type String + new line. */
public static void affConsoleStringRetourLigne(String mot)
{
System.out.println(mot);
}
/* Display a value of type long + new line. */
public static void affConsoleLongRetourLigne(long nombre)
{
System.out.println(nombre);
}
/* Display input and convert it to long. */
public static long saisieLong(String Phrase)
{
return Long.parseLong(JOptionPane.showInputDialog(Phrase));
}
/* Sum cubic numbers. */
public static long sommeDesCubesDesChiffres(long n)
{
long res = 0, tmp, tmp1;
// TODO: cleanup 'tmp'.
tmp = n;
do {
tmp1 = (tmp - (tmp/10) * 10);
tmp = (long)Math.floor(tmp/10);
res = res + (long)Math.pow(tmp1, 3);
} while (tmp > 0);
return res;
}
/* n cubic ? */
public static Boolean estCubique(long n)
{
if (n == sommeDesCubesDesChiffres(n))
return true;
else
return false;
}
public static void rechercheDesNombresCubiques(long n)
{
affConsoleStringRetourLigne("Les nombres cubiques " +
"inférieurs à n sont:");
long i;
for (i=1; i<n; i++)
{
if (estCubique(i))
affConsoleLongRetourLigne(i);
}
affConsoleStringRetourLigne("Voilà voilà voilà!");
}
public static void main(String[] args)
{
if (JOptionPane.showConfirmDialog(null, "Recherche " +
"des entiers cubiques inférieurs à un entier" +
" donné.\n\nVoulez-vous continuer ?", "Confirmation",
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
System.exit(0);
}
long n;
/*
* Setup a loop so that we'll keep asking for 'n' until a
* value <3000 is given.
*/
do
{
n = saisieLong("Donner un entier n (< 3000):");
} while ((n >= 3000) || (n < 0));
rechercheDesNombresCubiques(n);
}
}
@earth75
Copy link

earth75 commented Oct 29, 2012

Pourquoi ne pas compacter ça en une fonction qui renvoie un long? Concaténation FTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment