vb.net - pascal triangle gives overflow for 13 -


i wrote code output pascal triangle in multi-line textbook. program works fine inputs between 1 12 gives overflow error once value of 13 inputed.

is there modifications can make enable program accurately give outputs 13 , higher?

here code used:

public class pascal_triangle      private function factorial(byval k integer) integer         if k = 0 or k = 1             return 1         else             return k * factorial(k - 1)         end if     end function       private sub btngen_click(sender object, e eventargs) handles btngen.click         dim ncr integer         dim i, j, k integer         dim output string          output = ""          j = val(txtcolumn.text)          k = 0 j             = 0 k                 dim fact, fact1, fact2 integer                  fact = factorial(k)                 fact1 = factorial(k - i)                 fact2 = factorial(i)                 ncr = fact / (fact1 * fact2)                 txtoutput.text += str(ncr) & output             next             txtoutput.text += vbcrlf         next     end sub  end class 

the overflow because 13! big fit in integer.

the largest representable integer (32-bit signed)

  • 2147483647 (0x7fffffff == 01111111 11111111 11111111 11111111b)

so :

 12!    =  479001600    maxint = 2147483647  13!    = 6227020800     

if want use larger numbers need use larger number type. next larger types long (64-bit signed, max 9223372036854775807) or, purposes, ulong (unsigned 64-bit, since don't need negative numbers, twice @ 18446744073709551615).

this let calculate to20!, 2432902008176640000. numbers larger need using either biginteger or other dedicated libraries allow holding , calculating arbitrarily large numbers.

alternatively, can other methods of computing arbitrary row without using factorials.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -