001    package de.hska.java.aufgaben.kontrollstrukturen;
002    
003    /**
004     * Findet abundante und vollkommene Zahlen.
005     * <ul>
006     *   <li>
007     *    <em>Abundante Zahl:</em>: eine natürliche Zahl dessen Summe aller Teiler
008     *                      dieser Zahl außer der Zahl selbst 
009     *          <em>größer</em> ist als die Zahl.
010     *   </li>
011     *   <li>
012     *    <em>Vollkommene Zahl:</em>: Wenn diese Summ <em>gleich</em> der Zahl ist.
013     *   </li>
014     * </ul>
015     * 12 &lt; 1 + 2 + 3 + 4 + 6 = 16 ist eine abundante Zahl. 6 = 1 + 2 + 3 
016     * ist eine vollkommen Zahl.
017     * 
018     * <p>
019     *   <a href="http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/kontrollanweisungen.html#abundante_zahl">Zurück zum Aufgabentext</a>
020     * </p>
021     * 
022     * @author Christian Pape
023     *
024     */
025    public class AbundanteZahl {
026    
027            /**
028             * Gibt alle vollkommenen und ungeraden
029             * Zahlen von 1 bis 1000 aus.
030             */
031            public static void main(String[] args) {
032                    for (long zahl = 1; zahl <= 1000; zahl++) {
033                            if (zahl % 2 != 0 && isAbundanteZahl(zahl)) {
034                                    System.out.println("Abundant  : " + zahl);
035                            } else if (isVollkommeneZahl(zahl)) {
036                                    System.out.println("Vollkommen: " + zahl);
037                            }
038                    }
039            }
040    
041            /**
042             * Gibt genau dann <code>true</code> zurück, wenn die <code>zahl</code>
043             * eine vollkommene Zahl ist.
044             * 
045             * @param zahl eine positive Zahl
046             */
047            public static boolean isVollkommeneZahl(long zahl) {
048                    return zahl == getSummeAllerTeiler(zahl);
049            }
050            
051            /**
052             * Gibt genau dann <code>true</code> zurück, wenn die <code>zahl</code>
053             * eine abundante Zahl ist.
054             * 
055             * @param zahl eine positive Zahl
056             */
057            public static boolean isAbundanteZahl(long zahl) {
058                    return zahl < getSummeAllerTeiler(zahl);
059            }
060            
061            /**
062             * Gibt die Summer aller Teiler der gebebenen <code>zahl</code>
063             * (inklusive 1, aber ohne den Teiler <code>zahl</code>) zurück.
064             * 
065             * @param zahl eine positive Zahl
066             */
067            private static int getSummeAllerTeiler(long zahl) {
068                    int summeAllerTeiler = 1;
069                    
070                    for (int teiler = 2; teiler < zahl; teiler++) {
071                            if ( zahl % teiler == 0) {
072                                    summeAllerTeiler += teiler;
073                            }
074                    }
075            
076                return summeAllerTeiler;
077                    
078            }
079    }