Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. //
  2. // ViewController.mm
  3. // CarmichaelNumber
  4. //
  5. // Created by MizushimaYusuke on 8/31/15.
  6. // Copyright (c) 2015 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #include <vector>
  11. #include <memory>
  12. using namespace std;
  13.  
  14. class CarmichaelNumber {
  15. public:
  16. vector<int> find(int n) {
  17. vector<int> numbers;
  18. for (uint64_t i=3; i<=n; i++) {
  19. BOOL isCarmichaelNumber = true;
  20. for (uint64_t j=2; j<i; j++) {
  21. if (modpow(j, i, i) != (j % i) || isPrime((int)i)) {
  22. isCarmichaelNumber = false;
  23. break;
  24. }
  25. }
  26. if (isCarmichaelNumber) numbers.emplace_back(i);
  27. }
  28. return numbers;
  29. }
  30. private:
  31. template <typename T>
  32. T modpow(T base, T exp, T mod) {
  33. T res = 1;
  34. while (exp > 0) {
  35. if (exp & 1) res = (res * base) % mod;
  36. base = (base * base) % mod;
  37. exp >>= 1;
  38. }
  39. return res;
  40. }
  41. bool isPrime(int n) {
  42. for (int i=2; i * i <= n; i++) {
  43. if (n % i == 0) return false;
  44. }
  45. return n != 1;
  46. }
  47. };
  48.  
  49. @interface ViewController ()
  50. @end
  51.  
  52. @implementation ViewController
  53.  
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56.  
  57. self.view.backgroundColor = [UIColor darkGrayColor];
  58.  
  59. UIView *rView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds))];
  60. rView.backgroundColor = [UIColor colorWithHue:0 saturation:0.65 brightness:0.7 alpha:1];
  61. [self.view addSubview:rView];
  62. }
  63.  
  64. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  65. shared_ptr<CarmichaelNumber> cppClass(new CarmichaelNumber());
  66. vector<int> res = cppClass->find(4000);
  67.  
  68. [self show:res to:4000];
  69. }
  70.  
  71. - (void)show:(vector<int>)numbers to:(int)to{
  72.  
  73. if (to == 0)
  74. return;
  75.  
  76. UILabel *l = [[UILabel alloc] init];
  77. l.font = [UIFont systemFontOfSize:30];
  78. l.textColor = [UIColor whiteColor];
  79. l.text = [NSString stringWithFormat:@"%d", to];
  80. [l sizeToFit];
  81. l.center = CGPointMake(CGRectGetMidX(self.view.bounds) * 0.5, -50);
  82. [self.view addSubview:l];
  83.  
  84.  
  85. if(find(numbers.begin(), numbers.end(), to) != numbers.end()) {
  86. [UIView animateKeyframesWithDuration:0.1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
  87. l.center = CGPointMake(l.center.x, numbers.size() * 100);
  88. } completion:^(BOOL finished) {
  89. [UIView animateWithDuration:0.1 animations:^{
  90. l.center = CGPointMake(l.center.x * 3, numbers.size() * 100);
  91. }];
  92. }];
  93.  
  94. numbers.erase(find(numbers.begin(), numbers.end(), to));
  95.  
  96. } else {
  97. [UIView animateKeyframesWithDuration:0.2 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
  98. l.center = CGPointMake(l.center.x, 800);
  99. } completion:^(BOOL finished) {
  100. [l removeFromSuperview];
  101. }];
  102. }
  103.  
  104.  
  105.  
  106. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  107. [self show:numbers to:to - 1];
  108. });
  109. }
  110.  
  111. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement