Advertisement
Guest User

RationalToInteger

a guest
Aug 29th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. public class RationalToInteger {
  2.     public static void main(String... args){
  3.         int count=0;
  4.         for(int base=1;base<=8;base++){
  5.             for(int sub=1;sub<=base*base;sub++){
  6.                 System.out.println("==Raw ["+(++count)+"]==");
  7.                 System.out.println("Value= "+(sub/(double)base));
  8.                 System.out.println("Short #"+getOrder(sub,base));
  9.                 System.out.println();
  10.             }
  11.         }
  12.         System.out.println("==Customs==");
  13.         System.out.println("Short #"+getOrder(71, 6));
  14.         System.out.println();
  15.         System.out.println("Short #"+getOrder(19, 2));
  16.         System.out.println();
  17.     }
  18.     public static int getOrder(int top,int bottom){
  19.         if(top==0) return 0;
  20.         if(top<0) return -getOrder(-top,bottom);
  21.         if(bottom<0) return -getOrder(top,-bottom);
  22.         if(bottom==0) return 0;
  23.         System.out.println("old fraction= "+top+"/"+bottom);
  24.         double num=gcd(top,bottom);
  25.         double a=top/num;
  26.         double b=bottom/num;
  27.         System.out.println("reduced fraction= "+(int)a+"/"+(int)b);
  28.         double k=Math.ceil(a/(b*b));
  29.         double n=a*k;
  30.         double t=b*k;
  31.         System.out.println("mult= "+(int)k);
  32.         System.out.println("mult fraction= "+(int)n+"/"+(int)t);
  33.         return sumSquares((int)t-1)+(int)n;
  34.     }
  35.     public static int gcd(int a,int b){
  36.         if(a<=0 || b<=0) return 0;
  37.         if(a==1 || b==1) return 1;
  38.         if(a>b){
  39.             int t=a; a=b; b=t;
  40.         }
  41.         while(true){
  42.             int r=b%a;
  43.             if(r==0) return a;
  44.             b=a;
  45.             a=r;
  46.         }
  47.     }
  48.     public static int sumSquares(int n){
  49.         return n*(n+1)*(2*n+1)/6;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement