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

Deficient Number 

A deficient number is a number for which the sum of its divisors, is less than itself.

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

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

/**
 * A deficient number has a sum of divisors that is smaller than itself
 
 @author Sudhakar KV
 *
 */
public class DeficientNumber {

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

  private static boolean isDeficientNumber(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 deficient = (sumOfDevisors < number);
    System.err.println(number + " is " 
        (deficient ? "a deficient number" "not a deficient number"
        " " + getSum(devisors, sumOfDevisors));
    return deficient;
  }
  
  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 not a deficient number [ 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 ]
60 is not a deficient number [ 1 + 2 + 3 + 4 + 5 + 6 + 10 + 12 + 15 + 20 + 30 = 108 ]
100 is not a deficient number [ 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 ]
52 is a deficient number [ 1 + 2 + 4 + 13 + 26 = 46 ]
124 is a deficient number [ 1 + 2 + 4 + 31 + 62 = 100 ]
9 is a deficient number [ 1 + 3 = 4 ]
128 is a deficient number [ 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127 ]



 
  


  
bl  br