Skip to content
리눅스/BSD

생코보다_만든c언어libgmp사용_팩토리얼.c

by 흑엽 posted Jul 31, 2015

리눅스 C언어에서 [엄청~큰 숫자형]을 지원하는 라이브러리를 루트권한에서 설치!
apt-get install libgmp-dev
==============


팩토리얼이란?
https://ko.wikipedia.org/wiki/%EA%B3%84%EC%8A%B9
===========


$ cat 생코보다_만든c언어libgmp사용_팩토리얼.c
#include <stdio.h>
#include <gmp.h>
#include <time.h>
// https://kldp.org/node/129116 여기서 글올리신 분의 코드중에 설명이 필요한 부분에 주석달고, 코드중에는 출력과 입력부분을 수정했습니다.
void factorial(mpz_t* f, unsigned int n)
{
    unsigned int i = 1;

    for (i = 1; i <= n ; i++){ // 파라미터에서 정한 개수 만큼 반복해서 팩토리얼 하는 중. 
        mpz_mul_si(*f, *f, i); // http://gmplib.org/manual/Integer-Arithmetic.html#index-mpz_005fmul_005fsi-285
    }
}

int main(int argc, char **argv)
{
    mpz_t facN; //엄청~큰 변수
    unsigned int n = atol(argv[1]);// 파라미터 1번의 값으로 총 팩토리얼 개수를 정함 
    clock_t start, end;
    double runtime;


    mpz_init(facN); // https://gmplib.org/manual/Initializing-Integers.html#index-mpz_005finit-244
    mpz_set_ui(facN, 1L); //엄청~큰 변수 객체 초기화 등등 https://gmplib.org/manual/Assigning-Integers.html#index-mpz_005fset_005fui-253

   

    start = clock(); //계산시작 타이머
    factorial(&facN, n);//반복해서 팩토리얼 하는 함수 호출
    end = clock(); //계산끝 타이머 종료

   

    runtime = (double)(end-start) / CLOCKS_PER_SEC;
    printf ("실행시간  %f초\n", runtime);
    printf ("이하 파라미터로 지정된 %u의 팩토리얼 계산결과", n);
    mpz_out_str(stdout, 10, facN); //계산완료된 변수를 출력하는 이부분에서... 위의 runtime보다 시간이 더 소모될수 있음. 
    printf("\n");

    mpz_clear(facN);
}
=========


컴파일 명령어
gcc -o생코보다_만든c언어libgmp사용_팩토리얼 생코보다_만든c언어libgmp사용_팩토리얼.c -lgmp
============


실행명렁어
 ./생코보다_만든c언어libgmp사용_팩토리얼 1000000 > 생코보다_만든c언어libgmp사용_팩토리얼result.txt &
============


실행결과->생코보다_만든c언어libgmp사용_팩토리얼result.txt의 내용
실행시간  308.84초(실험용으로 쓰는 vmware에서 돌렸기 때문에, 빠른 컴퓨터에서는 더 좋은 결과가 나올듯)
이하 파라미터로 지정된 1000000의 팩토리얼 계산결과 --> txt파일 용량만 5.8매가이고, 556만자리라 로딩이 안될까봐  유툽 동영상으로 올림. http://youtu.be/1J_nmOL3Mmo


일상

드론 배달시 추락사고에 대한 개인적인 우려 입니다.

by 흑엽 posted Jul 30, 2015

아마존은 이러한 높은 고도에서 운용하는 고속 드론의 허가를 주장을 하고 있습니다-> http://news.nate.com/view/20150729n09468
프롭 한개 고장으로 나머지 프롭으로 회전하면서 하강하다가 다른 드론과 충돌하여 추락시 차량파손이나 인명피해가 발생하면 정해진 비행경로로 제한되겠죠. [아파트 옥상에서 초딩들이 행인들에게 풍선 물폭탄을 던지다가 중력에너지 생각못하고 차를 찌그러트리고 유리창을 파손시키고, 코뼈를 부러트리고 실명 위기로 전치8주 부상등등의 사고들]을 볼때 배달물 추락시는 매우 위험하죠. http://imnews.imbc.com/20dbnews/history/2001/1876451_13453.html
http://www.imaeil.com/sub_news/sub_news_view.php?news_id=33166&yy=2003

그래서 저는 고도 2미터의 초저공 비행전용 도로에서 드론이 다녀야 한다는 생각입니다;;; 하천 위 등등을 지정해서요.. 알고리즘 오류나 모터고장과 눈먼새의 충돌등등의 상황도 고려해야 하니까요. 무인자동차가 고장나면 정지하지만, 무인드론의 경우 고장시 중력에너지에 의한 추락급발진의 시작이니까요.
하천통합정보 시스템을 사용한 드론 도로 지정이라는 상상도 해봅니다. 개인적으로 가장 적당한 드론 비행 경로라고 생각하기 때문에요. http://garam.kwater.or.kr/page.do?page_val=2&page_id=2_1&region=%ED%95%9C%EA%B0%95%EA%B6%8C%EC%97%AD&mosik_val=
미래 지하철에는 지하철 통로를 더 넓게 만들어서 드론길을 만들것 같다는 생각도 해봅니다.
수십년후에는 자기부상 자동차의 아랫고도로 배달 드론들이 다닐수도 있겠죠.

미국같은 경우 땅이 큰만큼 경로만 잘지키면 큰 문제가 없을 수 있겠죠.

http://news.nate.com/view/20150730n30352 어제 발생한 이런 사고를 보면서 여러가지 생각이 드네요. 3kg짜리 물풍선이 중력가속도로 100km 돌파하면 순간충격량이 5톤이라는데... 8.5k 짜리 드론이 부산 해운대 피서객의 머리위로 100km속도로 떨어졌다면..




일상

오늘 생코글보고 만들어본 엄청큰수 연속 더하기 입니다.

by 흑엽 posted Jul 30, 2015

알고리즘 없이 루프로 더하면 엄청 오래걸리지만

합의공식 사용하면 저의 4번째 개발테스트용 똥컴;;; vmware 리눅으로도 1초 안에 계산됩니다.


$ cat 생코보고_만들어본_엄청큰수_연속더하기.sh
#!/bin/sh
#시작과 끝값중에 홀수가 있으면 코드가 더 많아짐.
echo \(\($2+1\) \*\($2 / 2\)\) -  \(\($1 + 1\) \*\($1 / 2\)\) + $1 |bc

===


셀스크립트 해설

변수1부터_시작수까지_합 = (시작수+1) * (시작수 / 2)
변수1부터_끝나는수까지_합 = (끝나는수 + 1) * (끝나는수 / 2)
계산완료 = 변수1부터_끝나는수까지_합 - 변수1부터_시작수까지_합 + 시작수 #교수님이 태클걸으시면 알고리즘 없이 코딩할꺼냐고 항의하세요.

===


해당 프로그램 실행


$ ./생코보고_만들어본_엄청큰수_연속더하기.sh 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000               100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                   

===========


결과
4999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


Board Pagination Prev 1 ... 15 16 17 18 19 ... 61 Next
/ 61

Copyright ⓒ 성우 All rights reserved

Powered by SungWoo / Designed by SungWoo

sketchbook5, 스케치북5

sketchbook5, 스케치북5