Skip to content

Instantly share code, notes, and snippets.

@pratikone
Created December 11, 2013 09:43
Show Gist options
  • Save pratikone/7907657 to your computer and use it in GitHub Desktop.
Save pratikone/7907657 to your computer and use it in GitHub Desktop.
Multiply 2 million-digit numbers using files I/O
//
// simple.c
// MultiplyX
//
// Created by Pratik Anand on 02/12/13.
// Copyright (c) 2013 Pratik Anand. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
FILE *fp,*fp2,*fp3,*fp4;
fp=fopen("/Users/pratikone/Code/number", "w");
fp2=fopen("/Users/pratikone/Code/multiplier", "w");
fp4=fopen("/Users/pratikone/Code/result", "w");
int temp=0;
for (int i=0; i<3000; i++) {
temp=rand()%10;
fputc(temp+'0', fp);
}
for (int i=0; i<600; i++) {
temp=rand()%10;
fputc(temp+'0', fp2);
}
struct timeval tv_start,tv_end;
// insert code here...
printf("Hello, World!\n");
int lookup[10][10];
//lookup table
for(int l1=0;l1<=9;l1++)
for (int l2=0; l2<=9; l2++) {
lookup[l1][l2]=l1*l2;
}
//padding
for (int dd=0; dd<10000; dd++) {
//dSum[dd]='0';
fputc('0', fp4);
}
fclose(fp);
fclose(fp2);
fclose(fp4);
int nsize=3000;
int msize=600;
int ctrResJ=0;
char c,d;
int carry=0;
int res=0;
int d1=0,d2=0;
int dCarry=0;
int digit=0;
int dTemp=0;
int dRes=0;
int realCtr=0;
int real1=0,real2=0;
gettimeofday(&tv_start,NULL);
//tv_start.tv_sec ;// seconds
//tv_start.tv_usec; // microseconds
fp=fopen("/Users/pratikone/Code/number", "r");
fp2=fopen("/Users/pratikone/Code/multiplier", "r");
for (int ctr=0; ctr<=msize-1; ctr++) {
fp3=fopen("/Users/pratikone/Code/temp", "w");
for (int ctrNum=0;ctrNum<=nsize-1 ; ctrNum++) {
fseeko(fp, (-1*ctrNum)-1, SEEK_END);
fseeko(fp2, (-1*ctr)-1, SEEK_END);
real1=fgetc(fp)-'0';
real2=fgetc(fp2)-'0';
res=real1*real2;
//printf("%d*%d = %d\n",real1,real2,res);
//res=lookup[number[ctrNum]-'0'][multiplier[ctr]-'0'];
//adding the carry
res=res+carry;
carry=res/10;
res=res%10;
//printf("RESULT %c * %c = %d CARRY=%d\n",number[ctrNum],multiplier[ctr],res,carry );
//result[ctrResJ++]=res+'0';
fputc(res+'0', fp3);
//printf("\n in file: %c\n",res+'0');
res=0;
}
if(carry>0){
//printf("\ncarry: %c\n",carry+'0');
fputc(carry+'0', fp3);
//result[ctrResJ++]=carry+'0'; //last carry
carry=0;
}
//result[ctrResJ++]='\0';
fclose(fp3);
ctrResJ=0;
//add
d2=0;
fp3=fopen("/Users/pratikone/Code/temp", "r");
fp4=fopen("/Users/pratikone/Code/result", "r+");
while ((c=fgetc(fp3))!=EOF) {
//printf("\nRead from fp3 %c \n",c);
digit=c-'0';
fseeko(fp4, d2+realCtr, SEEK_SET);
//printf("\nLocation:%ld\n",ftell(fp4));
d=fgetc(fp4);
//fclose(fp4);
dTemp=d-'0';
//printf("\ndTemp %d \n",dTemp);
//dTemp=dSum[d2+realCtr]-'0';
dRes=(dTemp+digit+dCarry);
//printf("\ndRes %c \n",(dRes%10)+'0');
//fp4=fopen("/Users/pratikone/Code/result", "w");
fseeko(fp4, d2+realCtr, SEEK_SET);
fputc((dRes%10)+'0', fp4);
//fclose(fp4);
//dSum[d2+realCtr]=(dRes%10)+'0';
dCarry=dRes/10;
d2++;
}
if(dCarry>0){
//fp4=fopen("/Users/pratikone/Code/result", "w");
fseeko(fp4, d2+realCtr, SEEK_SET);
fputc(dCarry+'0', fp4);
//fclose(fp4);
//dSum[d2+realCtr]=dCarry+'0'; //last carry
dCarry=0;
d2++;
}
//copying for knowing the final end
d1=d2+realCtr;
d2=0;
//printf("Result is %s\n",dSum);
realCtr=realCtr+1;
fclose(fp3);
if (ctr==msize-1) {
fputc(EOF, fp4);
}
fclose(fp4);
}
fclose(fp);
fclose(fp2);
gettimeofday(&tv_end,NULL);
printf("\nTime taken :%ld sec\n",tv_end.tv_sec-tv_start.tv_sec);
printf("\nTime taken :%d us\n",tv_end.tv_usec-tv_start.tv_usec);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment