메뉴 건너뛰기

OBG

Programming

Algorithm
2012.08.02 21:06

큰 수 구하기 알고리즘

조회 수 658 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
http://funnism.com/34

참고 : 
http://gall.dcinside.com/list.php?id=internet_site&no=7410&page=2&bbs=


제가 직접 짠 큰 수 팩토리얼을 구하는 소스코드입니다. 최적화나 어떤 알고리즘을 쓴것은 아니고 막코딩입니다.
10000팩토리얼의 자리수가 3만자리정도 되기때문에 결과값이 저장될 벡터의 크기는 40000으로 잡았습니다. 
더 큰수의 팩토리얼을 구하려면 자리수를 늘려주면 됩니다.

하지만 초보자분들이나 안짜보신분들은 꼭 먼저 짜보시고, 성공하신 다음 소스코드를 참조하시기 바랍니다.
그것이 실력향상의 지름길이니까요...^^

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int>* vecData = new vector<int>;
 
    int length = 40000; // 결과값의 자리수
 
    vecData->reserve(length);
    vecData->assign(length, -1);
    int n = 1000;
    int carry = 0;
 
    (*vecData)[0] = 1;
    int offset = 1;
    for(int i=2;i <= n;i++){
        for(int j=0;j < offset;j++){
            (*vecData)[j] = (*vecData)[j] * i + carry;
            carry = 0;
            if((*vecData)[j] > 9){
                if((*vecData)[j+1] == -1){
                    offset++;
                    (*vecData)[j+1] = 0;
                }
                carry = (*vecData)[j] / 10;
                (*vecData)[j] = (*vecData)[j] % 10;
            }
        }
    }
 
    for(int i=0;i < offset;i++){
        cout << (*vecData)[offset-i-1];
    }
 
    delete vecData;
    return 0;
}

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 1728
27 LLM [번역]거대언어모델(LLM) 가이드 OBG 2023.07.20 71
26 Deeplearning Top 3 most used Pytorch Ecosystem Libraries you should Know about OBG 2023.08.02 86
25 Deeplearning LSTM-AE를 이용한 시퀀스 데이터 이상 탐지 OBG 2023.08.14 74
24 Deeplearning 내 마음대로 선정한 머신러닝/딥러닝 학습 추천 서적 OBG 2023.08.14 72
23 Deeplearning 마이크로소프트가 공개한 무료 AI 코스들 OBG 2023.11.28 56
22 Site 모든 개발자를위한 10 가지 특별한 GitHub 리포지토리 OBG 2023.12.28 110
21 Site 10 Useful/Fun/Weird Github Repos You Have to Play Around With OBG 2023.12.28 94
20 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 114
19 Deeplearning [ifkakao] 추천 시스템: 맥락과 취향 사이 줄타 OBG 2024.01.10 30
18 서버 멀티-플레이어 게임 서버와 레이턴시 보상 테크닉 OBG 2024.01.16 43
17 Deeplearning Using Machine Learning to Predict Customers’ Next Purchase Day OBG 2024.02.27 40
16 LLM [12월 1주] 떠오르는 '미스트랄 7B'...'라마 2' 이어 한국어 모델 세대교체 주도 OBG 2024.03.05 34
15 LLM A Beginner's Guide to Prompt Engineering with GitHub Copilot OBG 2024.04.04 19
14 LLM 만능 프롬프트 OBG 2024.04.07 27
13 LLM How LLMs Work ? Explained in 9 Steps — Transformer Architecture OBG 2024.04.11 21
12 LLM Getting Started with Sentiment Analysis using Python OBG 2024.04.11 30
11 LLM Real-Time Stock News Sentiment Prediction with Python OBG 2024.04.11 30
10 LLM ChatGPT의 강력한 경쟁 언어모델 등장!, Mixtral 8x7B OBG 2024.04.14 23
9 LLM Mixture of Experts - Part 2 OBG 2024.04.14 23
8 LLM The difference between quantization methods for the same bits OBG 2024.04.14 28
Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 ... 15 Next
/ 15
위로