4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ユニークな数値をいっぱい作ってみた

Posted at

はじめに

ユニークな数値、かつチェックサムがあるもの
というお題があったので関わってみました。

objective-c

Xcodeから新規でプロジェクトを作成します。
スクリーンショット 2014-06-03 19.46.16.png

main.m
//
//  main.m
//  fourNumber
//
//  Created by f-y on 2014/05/26.
//  Copyright (c) 2014年 f-y. All rights reserved.
//

# import <Cocoa/Cocoa.h>

//#define MODE_DEBUG

int main(int argc, const char * argv[])
{
    //計測用
    NSDate *startDate;
    NSDate *endDate;
    
    // 現在時間を代入
    startDate = [NSDate date];
    int i, n;
    NSMutableDictionary * mdic = [[NSMutableDictionary alloc]init];
    
    i=0;
    do {
        n = arc4random_uniform(1000) ;
        if (n==0) continue; //0は使わない
        
        //セットしているobjectは適当
        [mdic setObject:@"1" forKey:[NSString stringWithFormat:@"%03d", n]];
# ifdef MODE_DEBUG
        i++;
        NSLog(@"%3d回目 = %03d", i, n);
# endif
    } while ([mdic count]<500);
    
# ifdef MODE_DEBUG
    NSLog(@"count %lu",(unsigned long)[mdic count]);
# endif
    i=0;
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (NSString * key in mdic) {
        //------- CheckSum
        int oddSum = [[key substringWithRange:NSMakeRange(0,1)] intValue] + [[key substringWithRange:NSMakeRange(2,1)] intValue];
        int evenSum = [[key substringWithRange:NSMakeRange(1,1)] intValue];
        int checkNO = (oddSum * 3 + evenSum)%10;
# ifdef MODE_DEBUG
        NSLog(@"oddSum * 7 + evenSum: %d,  checkNO %d", oddSum * 3 + evenSum,checkNO);
# endif
        [array addObject:[NSString stringWithFormat:@"%@%d",key ,checkNO]];
        
# ifdef MODE_DEBUG
        //------- Result
        i++;
        NSLog(@"%4d回目 = %@ checkNO: %d",i,key,checkNO);
# endif
    }
    // 処理終了位置で現在時間を代入
    endDate = [NSDate date];
    NSArray * a = [array copy];
    
    // 開始時間と終了時間の差を表示
    NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
    
    // 日付フォーマットオブジェクトの生成
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    // フォーマットを指定の日付フォーマットに設定
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
    // 日付の文字列を生成
    
    NSLog(@"開始時間 = %@",[dateFormatter stringFromDate:startDate]);
    NSLog(@"終了時間 = %@",[dateFormatter stringFromDate:endDate]);
    NSLog(@"経過時間 = %.3f秒",interval);
    NSLog(@"-------------------------------");
    NSLog(@"配列の数 = %lu",[a count]);
    NSLog(@"%@",[a componentsJoinedByString:@","]);
}

デバッグ時には#define MODE_DEBUGのコメントを外します。
NSLogをつけているとだいぶ時間がかかるのが分かります。

mysql

XAMPPがあったので、mysqlAdminから確認しつつ、です。

先に

> CREATE TABLE t (i INT);
> INSERT INTO t VALUES(1),(2),(3), ... , (1000);#ここは...の部分も1000個値を記述します

INSERTする値はExcelで横にだーっ!と1000列値をつくって
テキストに貼付けて成形しました。

tekito.sql
SELECT distinct value FROM 
(
SELECT randNO,
( (substring(randNO,1,1) +  substring(randNO,3,1))*3 + substring(randNO,2,1)) % 10 as checksum ,
concat(randNO,( (substring(randNO,1,1) +  substring(randNO,3,1))*3 + substring(randNO,2,1)) % 10) as value
 FROM(
SELECT t.i, lpad(FLOOR(1 + (RAND() * 998)),3,'0') as randNO
 FROM t
) tmp

) randValues
LIMIT 500

さいごに

普段あまり調べない領域を復習できてよかったなと
自己満足ついでにアップしただけです。ごめんなさい

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?