Skip to content

Instantly share code, notes, and snippets.

@vikingosegundo
Last active January 2, 2016 03:19
Show Gist options
  • Save vikingosegundo/8243075 to your computer and use it in GitHub Desktop.
Save vikingosegundo/8243075 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
NSUInteger euclid(NSUInteger a, NSUInteger b)
{
while (b > 0) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
NSUInteger recursiveEuclid(NSUInteger a, NSUInteger b)
{
if(b == 0)
return a;
return recursiveEuclid (b, a % b);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSUInteger first = 1;
NSUInteger second = 2;
BOOL recursive = NO;
if ([[NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding] isEqualToString:@"-r"]) {
first = 2;
second = 3;
recursive = YES;
}
NSInteger x = [[NSString stringWithCString:argv[first] encoding:NSUTF8StringEncoding] integerValue];
NSInteger y = [[NSString stringWithCString:argv[second] encoding:NSUTF8StringEncoding] integerValue];
if (x < 1 || y < 1) {
NSLog(@"Werte aus ℕ mit a, b > 0 erlaubt");
return -1;
}
NSUInteger ggt = (recursive)? recursiveEuclid(x, y) : euclid(x, y);
x = x / ggt;
y = y / ggt;
NSLog(@"%ld quadratische Stücke (%ld x %ld) mit %ld cm Seitenlänge", x*y, x,y, ggt);
if (ggt > 1) {
NSLog(@"oder einfach quadratische Stücke mit 1 cm Seitenlänge");
}
}
return 0;
}
astridskuchenblech:
a,b ∈ ℕ\{0}
./astridskuchenblech a b
./astridskuchenblech -r a b
./astridskuchenblech 120 144
./astridskuchenblech -r 120 144
30 quadratische Stücke (5 x 6) mit 24 cm Seitenlänge
oder einfach quadratische Stücke mit 1 cm Seitenlänge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment