Java, 85 83 bytes
String f(int n,int d){return n<0?"-"+f(2*(d+n%d)-n,d):n/d+(n%d<1?"":","+f(d,n%d));}
Takes integer or fraction or decimal as String, 311 bytes
String c(String i){try{return""+Integer.decode(i);}catch(Exception e){int o=i.indexOf(46);if(o>=0)return c((i+"/"+Math.pow(10,i.length()-o-2)).replaceAll("\\.",""));String[]a=i.split("/");int n=Integer.decode(a[0]),d=Integer.decode(a[1]);return n<0?"-"+c(2*(d+n%d)-n+"/"+d):n%d<1?""+n/d:n/d+","+c(d+"/"+(n%d));}}
Sample input/output:
860438
860438
3.245
3,4,12,4
-4.2
-5,1,4
-114.7802
-115,4,1,1,4,1,1,5,1,1,4
0/11
0
1/42
0,42
2/7
0,3,2
-18/17056
-1,1,946,1,1,4
-17056/18
-948,2,4
Actual input/output from full program:
860438
860438
860438
860438
860438
3.245
3,4,12,4
3,4,12,4
3,4,12,4
3,4,12,4
-4.2
-5,1,4
-5,1,4
-5,1,4
-5,1,4
-114.7802
-115,4,1,1,4,1,1,5,1,1,4
-115,4,1,1,4,1,1,5,1,1,4
-115,4,1,1,4,1,1,5,1,1,4
-115,4,1,1,4,1,1,5,1,1,4
0/11
0
0
0
0
1/42
0,42
0,42
0,42
0,42
2/7
0,3,2
0,3,2
0,3,2
0,3,2
-18/17056
-1,1,946,1,1,4
-1,1,946,1,1,4
-1,1,946,1,1,4
-1,1,946,1,1,4
-17056/18
-948,2,4
-948,2,4
-948,2,4
-948,2,4
Full program (including ungolfed functions):
import java.util.Scanner;
public class Q79483 {
String cf_ungolfed(String input){
try{
int result = Integer.parseInt(input);
return Integer.toString(result);
}catch(Exception e){
if(input.indexOf('.')>=0){
int numerator = Integer.parseInt(input.replaceAll("\\.",""));
int denominator = (int) Math.pow(10, input.length()-input.indexOf('.')-1);
return cf_ungolfed(numerator+"/"+denominator);
}
int numerator = Integer.parseInt(input.substring(0,input.indexOf('/')));
int denominator = Integer.parseInt(input.substring(input.indexOf('/')+1));
if(numerator%denominator == 0){
return Integer.toString(numerator/denominator);
}
if(numerator < 0){
return "-"+cf_ungolfed((denominator-numerator+(denominator+2*(numerator%denominator)))+"/"+denominator);
}
return (numerator/denominator) + "," + cf_ungolfed(denominator+"/"+(numerator%denominator));
}
}
String c(String i){try{return""+Integer.decode(i);}catch(Exception e){int o=i.indexOf(46);if(o>=0)return c((i+"/"+Math.pow(10,i.length()-o-2)).replaceAll("\\.",""));int n=Integer.decode(i.split("/")[0]),d=Integer.decode(i.split("/")[1]);return n<0?"-"+c(2*(d+n%d)-n+"/"+d):n%d<1?""+n/d:n/d+","+c(d+"/"+(n%d));}}
String f_ungolfed(int numerator,int denominator){
if(numerator%denominator == 0){
return Integer.toString(numerator/denominator);
}
if(numerator < 0){
return "-"+f_ungolfed((denominator-numerator+(denominator+2*(numerator%denominator))),denominator);
}
return (numerator/denominator) + "," + f_ungolfed(denominator,(numerator%denominator));
}
String f(int n,int d){return n<0?"-"+f(2*(d+n%d)-n,d):n/d+(n%d<1?"":","+f(d,n%d));}
public static int[] format(String input){
if(input.indexOf('.') != -1){
int numerator = Integer.parseInt(input.replaceAll("\\.",""));
int denominator = (int) Math.pow(10, input.length()-input.indexOf('.')-1);
return new int[]{numerator,denominator};
}
if(input.indexOf('/') != -1){
int numerator = Integer.parseInt(input.substring(0,input.indexOf('/')));
int denominator = Integer.parseInt(input.substring(input.indexOf('/')+1));
return new int[]{numerator,denominator};
}
return new int[]{Integer.parseInt(input),1};
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String input = sc.next();
System.out.println(new Q79483().cf_ungolfed(input));
System.out.println(new Q79483().c(input));
int[] formatted = format(input);
System.out.println(new Q79483().f_ungolfed(formatted[0], formatted[1]));
System.out.println(new Q79483().f(formatted[0], formatted[1]));
}
sc.close();
}
}
Related: Determining the continued fractions of square roots
– mbomb007 – 2016-05-05T22:02:31.7172Can the input be required to be a fraction/rational type (which would require inputting the decimal
3.245
as3245/1000
or similar)? – Doorknob – 2016-05-05T22:05:48.7201Mathematica wins >:| – Conor O'Brien – 2016-05-05T22:14:07.457
1What if there are several solutions? Is
'3' '2' '3' '-6'
acceptable for3.245
? Also, are leading0
convergents accepted? – Luis Mendo – 2016-05-05T22:17:28.487@LuisMendo What method is used to find alternate solutions? The program I created only finds positive convergents. – mbomb007 – 2016-05-05T22:37:47.737
@mbomb007 I use a Matlab function which produces negative ones. But I think the standard represwentation requires positive ones except for possibly the first – Luis Mendo – 2016-05-05T22:40:28.190
@LuisMendo Just positive only would be great, since that's easier for me to verify. – mbomb007 – 2016-05-05T22:41:34.287