Announcement

Collapse
No announcement yet.

Need help with Programming / Math

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with Programming / Math

    The assignment is to simulate the BigInteger class in Java using an array that represents the integer.
    For example the array [5,4,3,2,1] would be equal to 12345, where the 0 element is the 5.

    We have to do methods for addition, subtraction, multiplication, and division. I have already done add, subtract, and multiply, but now I'm stuck on division. I can't come up with a way to do this. I don't see long division working because I'd have to take the numbers out of the array and make it a real integer, but that won't work because the array can be quite large. I don't want to repeatedly subtract the divisor, although I might do it if I can't come up with another solution.

    I'm trying to figure something out where each element is considered a power of 10. Such as 123 = 1x^2 + 2x + 3, where x = 10, but I still can't figure anything out. Synthetic division won't work since the divisor isn't always a monomial. Anyone know of an algorithm that can do this? Can be in any language besides Java, I just need an algorithm.

    Big Thanks,
    Thien
    Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
    Motion Computing M1400 -- Tablet PC, Ubuntu Linux

    "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

  • #2
    Heh, I remember writing this in Pascal years ago.

    Don't do true division. Do addition/multiplication. If you want x/y = z, and x and y are known, keep doing x' = x' + y, and count how many times you have to do it (you get z). You can optimize this by guesstimation, search algorithms, etc.
    Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

    Comment


    • #3
      Yeah, that is similiar to repeatedly subtracting y from x. I was hoping there would be a better way, but I'm starting to think not.
      Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
      Motion Computing M1400 -- Tablet PC, Ubuntu Linux

      "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

      Comment


      • #4
        yes, it's similar, but
        1) Adding has the potential to be faster.
        2) Optimizing is easier.
        Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

        Comment

        Working...
        X