Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Created October 26, 2012 16:39
Show Gist options
  • Save adrientetar/3959802 to your computer and use it in GitHub Desktop.
Save adrientetar/3959802 to your computer and use it in GitHub Desktop.
Nombres cubiques (v1.1)
/**
* Ce programme calcule les entiers cubiques inférieurs
* à un entier donné (inférieur à 3000).
* @author Adrien Tétar
* @version 1.1
*
* Changelog:
* 1.1 - 'tmp', 'tmp1' cleanup.
*
* 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;
do {
res += (long)Math.pow((n - (n/10) * 10), 3);
n = (long)Math.floor(n/10);
} while (n > 0);
return res;
}
/* 'n' cubic ? */
public static Boolean estCubique(long n)
{
if (n == sommeDesCubesDesChiffres(n))
return true;
else
return false;
}
/* Find cubic numbers < 'n'. */
public static void rechercheDesNombresCubiques(long n)
{
affConsoleStringRetourLigne("Les nombres cubiques " +
"inférieurs à n sont:");
for (long 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment