An alternative algorithm for multiplying very large integers(unlimited precision) in fortran 90 !!

It was then in 2005 that I wanted to multiply two very large integers on a 32 bit machine.

089312830830809287302983 x 08302810398201373902380208 = Inf
or, Something in 1e+Blah Blah.

Converting to double and do the multiplication seemed to work, but I lost precision. Even when using LONG LONG DOUBLE available in Intel Fotran 90 did not seemed to work. And I did not know Java had this bigInteger and int.multiply(int1) in those days.

So, I developed application to do arbirtary precision mathematics …

So, I broke large numbers into chunks of chars and used decimal multiplication to form my unlimited large numbers in Fortran 90 and did mathematical calculations !!

And here is the code ..

 

Integer Computation with unlimited precision
  1 !
  2 ! CHANGE : PROTECT THE SCOPING INPUTS INTO THE ROUTINES
  3 !
  4 module utils
  5   implicit none
  6   logical,parameter :: debug=.false.
  7   integer,parameter :: maxlen=9999     ! the maximum len supported
  8 contains
  9   subroutine extract_num(chr,num)
 10     character(*),intent(in) :: chr
 11     integer,intent(out) :: num
 12     integer :: i
 13    ...
 25   end subroutine extract_num
 26   subroutine atoi(chr,num)
 27     character(*),intent(in) :: chr
 28     integer,intent(out) :: num
 29     character(len(chr)) :: tmp_chr
 30     integer :: i,n
 31     tmp_chr=chr
 32     num=0
 33     .......
 39   end subroutine atoi
 40   ! get varlen for the given fd position.
 41   subroutine get_varlen(varlen,fp,maxlen)
 42     integer :: varlen,fp
 43     integer :: maxlen,i
 44     character(maxlen) :: chr
 45     read(fp,*)chr
 46     !print*,'READ FROM FILE : ',chr
 47     .........
 52 end module utils
 53 
 54 ! MAIN PROGRAM BEGINS HERE .
 55 program main
 56   use utils
 57   integer :: a_len,b_len,fp
 58   fp=1
 59   open(unit=fp,file='INPUT.DAT',status='old')
 60   call get_varlen(a_len,fp,maxlen)
 61   call get_varlen(b_len,fp,maxlen)
 62   call process(a_len,b_len,fp)
 63   close(fp)
 64 contains
 65   ! subroutine process all. this is the one that
 66   ! actually reads the passed fp and asks for multiplication call
 67   subroutine process(a_len,b_len,fp)
 68     use utils
 69     integer,intent(in) :: fp,a_len,b_len
 70     character(a_len) :: a
 71     character(b_len) :: b
 72     character(2*(max(a_len,b_len))+1) :: s
 73     rewind(fp)
 74     read(fp,*)a
 75     read(fp,*)b
 76     call nmul(a,b,s)
 77     call pad_char(a) ; call pad_char(b) ; call pad_char(s)
 78     print*,'------------ARBITARY PRECISION MULTIPLICATION--------------'
 79     print*,a
 80     print*,b
 81     print*,s
 82     print*,'-----------------------------------------------------------'
 83     ! find the no,of digits .
 84     !print*,'no of digits : ',len(s)
 85   end subroutine process
 86 end program main
 87 
 88 ! program to adjust character array to righward , for arithematic.
 89 ! align the character to the right side
 90 ! SCOPING UNIT PROTECTION .
 91 subroutine right_align(char_array)
 92   use utils
 93   ........'
117   enddo
118 end subroutine right_align
119 
120 ! this subroutine performs addition of two arbitary array with
121 ! normal rules of arithematic. this is char to char add and
122 ! the result is also in character.
123 subroutine nadd(a,b,sum)
124   use utils
125   ....
147 end subroutine nadd
148 
149 ! this subroutine performs addition of two arbitary array with
150 ! normal rules of arithematic. this is char to char add and
151 ! the result is also in character.
152 subroutine nmul(a,b,prod)
153   use utils
154   ....
198 end subroutine nmul
199 
200 ! pad char .. to print only then meaningful char removing all
201 ! leading zeros. this will not modify the input.
202 subroutine pad_char(char_array)
203   use utils
204   .....
217 end subroutine pad_char
218 
219 ! this is the power routine . all computation on char only.
220 subroutine npow(chr_in,chr_pow,chr_out)
221   use utils
222   implicit none
223   .....
252 end subroutine npow
253

Please write to me at dubey.arpan@gmail.com for the full source code … Thanks.

Ouput :

10248392398748392384739827498327493824793874932473928472938742937429374932847

x

23907832187947392749382749837493287492837492387493287432974932874293742938749

=

2450168456655122066217920496312394663361416383268936569567270568154216988 \ 4809743275720693216000287865853798386109732733817934402490645593269063230 \
9188403.

This application is a rudimentary application of multiplication with decimal representation dissection and reconstruction. This application essentially provides an unlimited precision arithmetic controlled by variable maxlen :: 9999, which essentially is the maximum length. This can be increased to significant lengths. The max length that this application can easily provide precision till the limit of available ram in the host system !!

 

  1. I came to your Arbitary Precision Calculations | Arpan Dubey page and noticed you could have a lot more visitors. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get visitors from and they let you try the service for free. I managed to get over 300 targeted visitors to day to my website. {Check it out here:|Visit them http://bysb.eu/4rq9

Leave a Reply

Your email address will not be published. Required fields are marked *