Created
November 14, 2012 22:49
-
-
Save adrientetar/4075410 to your computer and use it in GitHub Desktop.
Nombres cubiques (v1.4)
This file contains 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.4 | |
* | |
* Changelog: | |
* 1.4 - reverted 1.2 and 1.3 changes. | |
* added sommeDesCubesDesChiffresS(). | |
* added rechercheDesNombresCubiquesS(). | |
* rechercheDesNombresCubiques*(): added sum. | |
* merged affConsole*() long + String. | |
* various minor fixes. | |
* 1.3 - added convertArrayListStr(). | |
* 1.2 - added rechercheDesNombresCubiquesI(). | |
* 1.1 - 'tmp', 'tmp1' cleanup. | |
* | |
* Copyright © 2012, Adrien Tétar. All rights reserved. | |
*/ | |
import javax.swing.JOptionPane; | |
class Nombres_cubiques | |
{ | |
/* Display a value of type String. */ | |
public static void affConsole(String mot) | |
{ | |
System.out.print(mot); | |
} | |
/* Display a value of type long. */ | |
public static void affConsole(long nombre) | |
{ | |
System.out.print(nombre); | |
} | |
/* Display a value of type String + new line. */ | |
public static void affConsoleRetourLigne(String mot) | |
{ | |
System.out.println(mot); | |
} | |
/* Display a value of type long + new line. */ | |
public static void affConsoleRetourLigne(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 { | |
/* | |
* (n - (n/10) * 10) will extract the | |
* last digit. | |
*/ | |
res += (long)Math.pow((n - (n/10) * 10), 3); | |
n = (long)Math.floor(n/10); | |
} while (n > 0); | |
return res; | |
} | |
/* Sum cubic numbers using strings. */ | |
public static long sommeDesCubesDesChiffresS(long n) | |
{ | |
long res = 0; | |
/* | |
* We'll use a String here to extract numbers | |
* easily. | |
*/ | |
String ns = Long.toString(n); | |
for (int i=0; i<ns.length(); i++) | |
/* | |
* Since String uses ASCII code in which: | |
* 0 is 48, | |
* 1 is 49, | |
* ... | |
* We'll subtract 48 to get the real | |
* numeric value. | |
*/ | |
res += (long)Math.pow((ns.charAt(i) - 48), 3); | |
return res; | |
} | |
/* Is 'n' cubic ? */ | |
public static Boolean estCubique(long n) | |
{ | |
// if (n == sommeDesCubesDesChiffres(n)) | |
if (n == sommeDesCubesDesChiffresS(n)) | |
return true; | |
else | |
return false; | |
} | |
/* Find cubic numbers < 'n' and sum them. */ | |
public static void rechercheDesNombresCubiques(long n) | |
{ | |
affConsoleRetourLigne("Les nombres cubiques " + | |
"inférieurs à n sont:"); | |
long sum = 0; | |
for (long i=1; i<n; i++) | |
{ | |
if (estCubique(i)) | |
{ | |
/* | |
* Add ", " _before_ appending value except | |
* for the first iteration. | |
* cf. stackoverflow.com/questions/285523 | |
*/ | |
if (sum > 0) | |
affConsole(", "); | |
affConsole(i); | |
sum = sum + i; | |
} | |
} | |
affConsoleRetourLigne("."); | |
affConsole("La somme de ces nombres fait "); | |
affConsole(sum); | |
affConsoleRetourLigne("."); | |
affConsoleRetourLigne("Voilà voilà voilà!"); | |
} | |
/* Find cubic numbers < 'n' and sum them, w/String + GUI. */ | |
public static void rechercheDesNombresCubiquesS(long n) | |
{ | |
String res = ""; | |
long sum = 0; | |
for (long i=0; i<n; i++) | |
{ | |
if (estCubique(i)) | |
{ | |
/* | |
* Add ", " _before_ appending value except | |
* for the first iteration. | |
* cf. stackoverflow.com/questions/285523 | |
*/ | |
if (res.length() != 0) | |
res = res + ", "; | |
res = res + i; | |
sum = sum + i; | |
} | |
} | |
JOptionPane.showMessageDialog(null, "Les nombres cubiques " + | |
"inférieurs à " + n + " sont :\n" + res + "." + | |
"\nLa somme de ces nombres fait:\n" + sum + "."); | |
} | |
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); | |
rechercheDesNombresCubiquesS(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment