tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Special Numbers > Abundant Number

Abundant Number 

An abundant number is a number for which the sum of its divisors, is larger than itself.

File Name  :  
com/bethecoder/tutorials/core/special_numbers/AbundantNumber.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.core.special_numbers;

import java.util.ArrayList;
import java.util.List;

/**
 * An abundant number has a sum of divisors that is larger than itself
 
 @author Sudhakar KV
 *
 */
public class AbundantNumber {

  /**
   @param args
   */
  public static void main(String[] args) {
    isAbundantNumber(36);
    isAbundantNumber(60);
    isAbundantNumber(100);
    isAbundantNumber(52);
    isAbundantNumber(124);
  }

  private static boolean isAbundantNumber(int number) {
    
    int sumOfDevisors = 0;
    List<Integer> devisors = new ArrayList<Integer>();
    for (int i = ; i <= number / ; i ++) {
      
      if (number % i == 0) {
        sumOfDevisors += i;
        devisors.add(i);
      }
    }
    
    boolean abundant = (sumOfDevisors > number);
    System.err.println(number + " is " 
        (abundant ? "an abundant number" "not an abundant number"
        " " + getSum(devisors, sumOfDevisors));
    return abundant;
  }
  
  private static String getSum(List<Integer> devisors, int sumOfDevisors) {
    
    StringBuilder sb = new StringBuilder("[ ");
    
    for (int i = ; i < devisors.size(); i ++) {
      
      if (i > 0) {
        sb.append(" + ");
      }
      
      sb.append(devisors.get(i));
    }
    
    sb.append(" = ").append(sumOfDevisors).append(" ]");
    return sb.toString();
  }
}
   

It gives the following output,
36 is an abundant number [ 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 ]
60 is an abundant number [ 1 + 2 + 3 + 4 + 5 + 6 + 10 + 12 + 15 + 20 + 30 = 108 ]
100 is an abundant number [ 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 ]
52 is not an abundant number [ 1 + 2 + 4 + 13 + 26 = 46 ]
124 is not an abundant number [ 1 + 2 + 4 + 31 + 62 = 100 ]



 
  


  
bl  br