Arithmetic/Rational/Objective-C: Difference between revisions
Content added Content deleted
m (prepare for inclusion as template) |
m (modernize) |
||
Line 11: | Line 11: | ||
BOOL withSign; |
BOOL withSign; |
||
} |
} |
||
+( |
+(instancetype)valueWithNumerator:(int)num andDenominator: (int)den; |
||
+( |
+(instancetype)valueWithDouble: (double)fnum; |
||
+( |
+(instancetype)valueWithInteger: (int)inum; |
||
+( |
+(instancetype)valueWithRational: (RCRationalNumber *)rnum; |
||
-(instancetype)initWithNumerator: (int)num andDenominator: (int)den; |
|||
-(id)init; |
|||
-( |
-(instancetype)initWithDouble: (double)fnum precision: (int)prec; |
||
⚫ | |||
-(id)initWithDouble: (double)fnum precision: (int)prec; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-(NSComparisonResult)compare: (RCRationalNumber *)rnum; |
-(NSComparisonResult)compare: (RCRationalNumber *)rnum; |
||
-(id)simplify: (BOOL)act; |
-(id)simplify: (BOOL)act; |
||
Line 63: | Line 62: | ||
@implementation RCRationalNumber |
@implementation RCRationalNumber |
||
// initializers |
// initializers |
||
-( |
-(instancetype)init |
||
{ |
{ |
||
NSLog(@"initialized to unity"); |
NSLog(@"initialized to unity"); |
||
Line 69: | Line 68: | ||
} |
} |
||
-( |
-(instancetype)initWithNumerator: (int)num andDenominator: (int)den |
||
{ |
{ |
||
if ((self = [super init]) != nil) { |
if ((self = [super init]) != nil) { |
||
Line 85: | Line 84: | ||
} |
} |
||
-( |
-(instancetype)initWithInteger:(int)inum |
||
{ |
{ |
||
return [self initWithNumerator: inum andDenominator: 1]; |
return [self initWithNumerator: inum andDenominator: 1]; |
||
} |
} |
||
-( |
-(instancetype)initWithDouble: (double)fnum precision: (int)prec |
||
{ |
{ |
||
if ( prec > 9 ) prec = 9; |
if ( prec > 9 ) prec = 9; |
||
Line 98: | Line 97: | ||
} |
} |
||
-( |
-(instancetype)initWithRational: (RCRationalNumber *)rnum |
||
{ |
{ |
||
return [self initWithNumerator: [rnum numerator] andDenominator: [rnum denominator]]; |
return [self initWithNumerator: [rnum numerator] andDenominator: [rnum denominator]]; |
||
Line 211: | Line 210: | ||
} |
} |
||
// or just test if |
// or just test if negative |
||
-(BOOL)isNegative |
-(BOOL)isNegative |
||
{ |
{ |
||
return |
return [self numerator] < 0; |
||
} |
} |
||
Line 253: | Line 252: | ||
// class method |
// class method |
||
+( |
+(instancetype)valueWithNumerator:(int)num andDenominator: (int)den |
||
{ |
{ |
||
return [[ |
return [[self alloc] initWithNumerator: num andDenominator: den]; |
||
} |
} |
||
+( |
+(instancetype)valueWithDouble: (double)fnum |
||
{ |
{ |
||
return [[ |
return [[self alloc] initWithDouble: fnum]; |
||
} |
} |
||
+( |
+(instancetype)valueWithInteger: (int)inum |
||
{ |
{ |
||
return [[ |
return [[self alloc] initWithInteger: inum]; |
||
} |
} |
||
+( |
+(instancetype)valueWithRational: (RCRationalNumber *)rnum |
||
{ |
{ |
||
return [[ |
return [[self alloc] initWithRational: rnum]; |
||
} |
} |
||
@end</lang> |
@end</lang> |
||
Line 280: | Line 279: | ||
int main() |
int main() |
||
{ |
{ |
||
@autoreleasepool { |
|||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|||
int i; |
int i; |
||
for(i=2; i < 0x80000; i++) { |
for(i=2; i < 0x80000; i++) { |
||
int candidate = i; |
int candidate = i; |
||
RCRationalNumber *sum = [RCRationalNumber valueWithNumerator: 1 |
RCRationalNumber *sum = [RCRationalNumber valueWithNumerator: 1 |
||
andDenominator: candidate]; |
andDenominator: candidate]; |
||
int factor; |
int factor; |
||
for(factor=2; factor < sqrt((double)candidate); factor++) { |
for(factor=2; factor < sqrt((double)candidate); factor++) { |
||
if ( (candidate % factor) == 0 ) { |
if ( (candidate % factor) == 0 ) { |
||
sum = [[sum add: [RCRationalNumber valueWithNumerator: 1 |
sum = [[sum add: [RCRationalNumber valueWithNumerator: 1 |
||
andDenominator: factor]] |
andDenominator: factor]] |
||
add: [RCRationalNumber valueWithNumerator: 1 |
add: [RCRationalNumber valueWithNumerator: 1 |
||
andDenominator: (candidate/factor)]]; |
andDenominator: (candidate/factor)]]; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
[pool release]; |
|||
return 0; |
return 0; |
||
}</lang> |
}</lang> |