Created
October 28, 2012 19:14
-
-
Save adrientetar/3969527 to your computer and use it in GitHub Desktop.
Nombres cubiques (v1.2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ce programme calcule les entiers cubiques inférieurs | |
* à un entier donné (inférieur à 3000). | |
* @author Adrien Tétar | |
* @version 1.2 | |
* | |
* Changelog: | |
* 1.2 - added rechercheDesNombresCubiquesI(). | |
* 1.1 - 'tmp', 'tmp1' cleanup. | |
* | |
* Copyright © 2012, Adrien Tétar. All rights reserved. | |
*/ | |
import java.util.ArrayList; | |
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à!"); | |
} | |
/* Find cubic numbers < 'n' w/GUI. */ | |
public static void rechercheDesNombresCubiquesI(long n) | |
{ | |
ArrayList<Long> res = new ArrayList<Long>(); | |
for (long i=1; i<n; i++) | |
{ | |
if (estCubique(i)) | |
res.add(i); | |
} | |
JOptionPane.showMessageDialog(null, "Les nombres cubiques " + | |
"inférieurs à n sont:\n" + convert(res) + | |
"\nVoilà 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); | |
rechercheDesNombresCubiquesI(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment