Results 1 to 3 of 3

Thread: Best way to do String Maths

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Berkhamsted, England
    Posts
    4

    Best way to do String Maths

    For some reason the other day I thought it would be fun to write a program which took two strings and returned the results of various maths operations. So far I've managed to do Adding and Multiplication. The first is quite simple, it just uses normal add and carry which you learn in your first arithmitic lessons at school. The multiplication function uses the method of long multiplication which most of us also learnt at school. I was just wondering if these were the most efficient techiniques taking into account that the numbers could have hundreds of digits.

    I thought to multiply one number by another I could just add one number to itself a certain number of times. However this would mean I'd have to make quite a few additions if I wanted to say multiply 4 billion by 3 billion. Long multiplication works better but it still doesn't seem that efficient. Anyone know of a better way?

    PS I've still yet to tackle subtraction and division, they seem a lot harder to implement. And if anyone wants to see my code (can add and multiply numbers with up to 2 billion digits (although that would take forever!) I'll try to post it some other time. I don't have access to my home computer from here and I don't have the net at home )

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    its probably a lot more efficient to store and operate the big numbers in binary form. what do you need the big numbers for anyway? If its just for fun then I wouldn't care about efficiency that much, still there are lots of implementation for big integers out there, just google.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are various algorithms to multiplicate large numbers, all of them very complicated.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width