Advertisement
StormWingDelta

Getting Factors of numbers (int and double)

Feb 4th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. public class Tester {
  2.  
  3.     public Tester() {
  4.         // TODO Auto-generated constructor stub
  5.         //http://stackoverflow.com/questions/9627182/how-do-i-limit-decimal-precision-in-processing
  6.     }
  7.  
  8.     /**
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {
  12.         // TODO Auto-generated method stub
  13.        
  14.         System.out.println(countFactors(10));
  15.         displayFactors(10);
  16.         float num = 145.378f;
  17.         System.out.println("Decimal Factors2: " + countFactorsD(num,3));
  18.         displayFactors(num,3);
  19.        
  20.     }
  21.    
  22.     public static double round2(double num, double places){return Math.round(num * Math.pow( 10.0, places ) ) / Math.pow( 10.0, places );}
  23.    
  24.     public static int countFactors(int number)
  25.     {
  26.         int count = 0;
  27.         for(int i = 1; i <= number; i++)
  28.         {
  29.             if(number % i == 0)
  30.             {
  31.                 count++;
  32.             }
  33.            
  34.         }
  35.         return count;
  36.     }
  37.     public static int[] getFactors(int number)
  38.     {
  39.         int count[] =  new int[countFactors(number)];
  40.         int arrayslot = 0;
  41.         for(int i = 1; i <= number; i++)
  42.         {
  43.             if(number % i == 0)
  44.             {
  45.                 count[arrayslot] = i;
  46.                 arrayslot++;
  47.             }
  48.            
  49.         }
  50.         return count;
  51.     }
  52.     public static void displayFactors(int number)
  53.     {
  54.         for(int i = 0; i < getFactors(number).length; i++)
  55.         {
  56.             System.out.print(getFactors(number)[i]+ " ");
  57.         }
  58.         System.out.println();
  59.     }
  60.    
  61.     public static double generateDCounterNS(int places)
  62.     {
  63.         double d = 1.0;
  64.         for(int i = 0; i <= places; i++)
  65.         {
  66.             if(i < places)
  67.             {
  68.                 d /= 10;
  69.             }
  70.             else
  71.             {
  72.                 //d /= 10;
  73.             }
  74.         }
  75.        
  76.         return d;
  77.     }
  78.    
  79.     public static int countFactorsD(float number, int places)
  80.     {
  81.         int count = 0;
  82.        
  83.         for(float i = (float)round2(generateDCounterNS(places),places); i <= (float)round2(number,places) + (float)round2(generateDCounterNS(places),places); i += (float)round2(generateDCounterNS(places),places))
  84.         {
  85.             if((float)round2(number,places) % (float)round2(i,places) == (float)round2(0.0,places))
  86.             {
  87.                 count++;
  88.             }
  89.             //System.out.println("Counter: " + (float)round2(i,1) + " What is " + number + " % " + i + " = " + (float)round2(number % (float)round2(i,1),1) );
  90.         }
  91.         return count;
  92.     }
  93.    
  94.     public static float[] getFactors(float number, int places)
  95.     {
  96.         float count[] =  new float[countFactorsD(number, places)];
  97.         int arrayslot = 0;
  98.         for(float i = (float)round2(generateDCounterNS(places),places); i <= (float)round2(number,places) + (float)round2(generateDCounterNS(places),places); i += (float)round2(generateDCounterNS(places),places))
  99.         {
  100.             if((float)round2(number,places) % (float)round2(i,places) == (float)round2(0.0,places))
  101.             {
  102.                 count[arrayslot] = (float)round2(i,places);
  103.                 arrayslot++;
  104.             }
  105.            
  106.         }
  107.         return count;
  108.     }
  109.     public static void displayFactors(float number, int places)
  110.     {
  111.         float[] numbers = getFactors(number, places);
  112.         for(int i = 0; i < numbers.length; i++)
  113.         {
  114.             System.out.print(numbers[i]+ " ");
  115.         }
  116.         System.out.println();
  117.     }
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement