Skip to content

Instantly share code, notes, and snippets.

@mredig
Created October 9, 2019 16:00
Show Gist options
  • Save mredig/13a5335b3178745000ee5d850846f408 to your computer and use it in GitHub Desktop.
Save mredig/13a5335b3178745000ee5d850846f408 to your computer and use it in GitHub Desktop.
//
// main.m
// Space Age
//
// Created by Michael Redig on 10/9/19.
// Copyright © 2019 Red_Egg Productions. All rights reserved.
//
#import <Foundation/Foundation.h>
double mercuryAge(double seconds) {
return (seconds / 31557600) / 0.2408467;
}
double venusAge(double seconds) {
return (seconds / 31557600) / 0.61519726;
}
double earthAge(double seconds) {
return seconds / 31557600;
}
double marsAge(double seconds) {
return (seconds / 31557600) / 1.8808158;
}
double jupiterAge(double seconds) {
return (seconds / 31557600) / 11.862615;
}
double saturnAge(double seconds) {
return (seconds / 31557600) / 29.447498;
}
double uranusAge(double seconds) {
return (seconds / 31557600) / 84.016846;
}
double neptuneAge(double seconds) {
return (seconds / 31557600) / 164.79132;
}
NSString* spaceAger(double seconds) {
NSString* mercury = [NSString stringWithFormat:@"%.02f Mercury years", mercuryAge(seconds)];
NSString* venus = [NSString stringWithFormat:@"%.02f Venus years", venusAge(seconds)];
NSString* earth = [NSString stringWithFormat:@"%.02f Earth years", earthAge(seconds)];
NSString* mars = [NSString stringWithFormat:@"%.02f Mars years", marsAge(seconds)];
NSString* jupiter = [NSString stringWithFormat:@"%.03f Jupiter years", jupiterAge(seconds)];
NSString* saturn = [NSString stringWithFormat:@"%.04f Saturn years", saturnAge(seconds)];
NSString* uranus = [NSString stringWithFormat:@"%.04f Uranus years", uranusAge(seconds)];
NSString* neptune = [NSString stringWithFormat:@"%.04f Neptune years", neptuneAge(seconds)];
return [NSString stringWithFormat:@"With %.0f seconds, you would be %@, %@, %@, %@, %@, %@, %@, and %@ old.", seconds, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString* spaceAge = spaceAger(31557600 * (31 + (9.0 / 365.0)));
NSLog(@"%@", spaceAge);
}
return 0;
}
@mredig
Copy link
Author

mredig commented Oct 9, 2019

@PaulSolt
Copy link

For clarity I would use a variable for the 31557600 value that's used numerous times. That's a magic number and prone to error if it's a literal number (not a constant).

@PaulSolt
Copy link

Always write NSString *mercury

Use more constants for 31, 9, 365

@mredig
Copy link
Author

mredig commented Oct 10, 2019

using a constants is a great idea.

not sure what you mean with mercury?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment