Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RationalToInteger {
- public static void main(String... args){
- int count=0;
- for(int base=1;base<=8;base++){
- for(int sub=1;sub<=base*base;sub++){
- System.out.println("==Raw ["+(++count)+"]==");
- System.out.println("Value= "+(sub/(double)base));
- System.out.println("Short #"+getOrder(sub,base));
- System.out.println();
- }
- }
- System.out.println("==Customs==");
- System.out.println("Short #"+getOrder(71, 6));
- System.out.println();
- System.out.println("Short #"+getOrder(19, 2));
- System.out.println();
- }
- public static int getOrder(int top,int bottom){
- if(top==0) return 0;
- if(top<0) return -getOrder(-top,bottom);
- if(bottom<0) return -getOrder(top,-bottom);
- if(bottom==0) return 0;
- System.out.println("old fraction= "+top+"/"+bottom);
- double num=gcd(top,bottom);
- double a=top/num;
- double b=bottom/num;
- System.out.println("reduced fraction= "+(int)a+"/"+(int)b);
- double k=Math.ceil(a/(b*b));
- double n=a*k;
- double t=b*k;
- System.out.println("mult= "+(int)k);
- System.out.println("mult fraction= "+(int)n+"/"+(int)t);
- return sumSquares((int)t-1)+(int)n;
- }
- public static int gcd(int a,int b){
- if(a<=0 || b<=0) return 0;
- if(a==1 || b==1) return 1;
- if(a>b){
- int t=a; a=b; b=t;
- }
- while(true){
- int r=b%a;
- if(r==0) return a;
- b=a;
- a=r;
- }
- }
- public static int sumSquares(int n){
- return n*(n+1)*(2*n+1)/6;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement