how can i large divide?

J

jty0734

i wanna large divide.

input is 1~1024 bit integer.

first, i think using sub is solution.

so i made two array. and sub divisor to dividend.

but this situation happen.

execute 10000000000 / 1 this calulation take so many time.

cause 10000000000 number of calculation are needed.

in large number, how can i divide calculation efficiently ??
 
J

Johannes Bauer

Johannes said:
TAOCP, Volume 2, Chapter 4.6.1, Algorithm D (page 421)

Oops, wrong. I meant:

TAOCP, Volume 2, Chapter 4.3.1, Algorithm D (page 272)

Regards,
Johannes
 
S

santosh

i wanna large divide.
input is 1~1024 bit integer.
first, i think using sub is solution.
so i made two array. and sub divisor to dividend.
but this situation happen.
execute 10000000000 / 1 this calulation take so many time.
cause 10000000000 number of calculation are needed.
in large number, how can i divide calculation efficiently ??

You might try using a bignum library like GMP. They employ several
algorithms to maximise speed and have special code for corner cases
like this.

Note: Your question has nothing to do with C, which is what this group
addresses. So you might want to try comp.programming in future for
questions on algorithms and general programming practises.
 
B

Bartc

Richard Heathfield said:
Malcolm McLean said:


His problem is that he is doing division by repeated subtraction ("first,
i
think using sub is solution"). To divide 10000000000 by 1 using repeated
subtraction, all you have to do is keep subtracting 1 from an object whose
value is originally 10000000000 until its value is 0, and count the number
of subtractions you have to perform. In this case, of course, that would
be 10000000000, which is a *lot* of subtractions. Whilst this method has
the virtue of simplicity, it is not spectacularly efficient.

Not for dividing by zero..

A simple check however will take care of dividing by 1, then the worst case
time is halved.
 
O

Owen Jacobson

Not for dividing by zero..

A simple check however will take care of dividing by 1, then the worst case
time is halved.

O (n) with smaller constant factors is still O (n).

-o
 
B

Bartc

Owen said:
O (n) with smaller constant factors is still O (n).

This O(n) notation has always been a mystery to me. I only ever measure
performance. And so did my boss and my users!

I don't know if I've got this right, but isn't the time taken by a car to
travel n miles always O(n)? In that case according to you, there's no
advantage in going at a faster rather than a slower speed? Or choosing one
car over another? Or choosing to walk instead!

Then this O(n) thing is even less use than I already thought.
 
R

Richard Bos

Bartc said:
Then this O(n) thing is even less use than I already thought.

It's very useful in theoretical computing science, and somewhat useful
if you have large sets of data. Most sets of data are small, though.

O-notation is useful, for example, for showing that for data sets larger
than a reasonable number, bubble sort is never going to be good enough
no matter how much you speed it up, because it is O(n*n) while better
sorts are O(n log n). If you can already prove that you're only going to
sort a list of less than 200 members, and do it only once every hour,
you can ignore such arguments. If you suspect you will (or may in the
future be going to) sort a list of 200000 members every five minutes,
that's when the usefulness of O-notation comes in.

Richard
 
B

Bartc

Richard said:
Bartc said:



No. For low values of n, sure, but as n increases you have to start
factoring in the performance degradation from having to haul

Yes, but in practice large n means motorway driving whereas small n means
town driving with traffic. I suppose it means real life is a bit different
from computer tasks.
The use, here, is to recognise that repeated subtraction is O(n), but
there are division methods that are *better* than O(n). I think I'm
right in saying that normal long division is O(log n).

In a more complex task it might be useful. In this case it's very obvious
what the bottleneck is.

I did actually write some C code to do this properly, with unsigned ints
(not posted).

And I'm guessing my code was O(log2 n) because it worked in binary. Would
this make it faster or slower than O(log n) or is there no difference?


Bartc
 
R

Richard Tobin

Bartc said:
And I'm guessing my code was O(log2 n) because it worked in binary. Would
this make it faster or slower than O(log n) or is there no difference?

The difference between log2 n and log10 n (or any logK n) is just a
constant factor, so it makes no difference to the order.

-- Richard
 
J

Johannes Bauer

Bartc said:
Then this O(n) thing is even less use than I already thought.

Yeah right. And that P/NP thing, what is that all about? Let's throw
that into the trash can all along. Finite elements? Galois fields? Reed
Solomon Codes? Ahh, never got them either, therefore they must be rubbish.

The unability of you to determine the usefullness of the Landau notation
is your deficit, not the notation's. I sure hope that you're never going
to evaluate algorithms which need to scale on a professional basis.

Regards,
Johannes
 
K

Keith Thompson

Richard Heathfield said:
Bartc said:



Big-O notation is a representation of computational complexity, not
runtime. The two are related, but not identical.

Let's say we have an O(6*n) algorithm. You can easily turn this into an
O(n) algorithm by buying a computer that works six times quicker than the
old one.
[...]

You can also do so without buying a faster computer.
O(6*n) *is* O(n).
 
B

Bartc

Johannes Bauer said:
Yeah right.

I should have said '...less use to me'.
And that P/NP thing, what is that all about? Let's throw

I'm not a mathematician. If there are people around who can understand this
stuff and it has useful applications then that's great.

The big O notation sounds useful if you're working with theory, but in the
practical world it's easy enough to measure this sort of thing.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Richard Heathfield said:
Bartc said:

<snip>

And I'm guessing my code was O(log2 n) because it worked in binary.
Would this make it faster or slower than O(log n) or is there no
difference?

Big-O notation is a representation of computational complexity, not
runtime. The two are related, but not identical.

Let's say we have an O(6*n) algorithm. You can easily turn this into an
O(n) algorithm by buying a computer that works six times quicker than
the old one.
[...]

You can also do so without buying a faster computer.
O(6*n) *is* O(n).

O(6*n) *is* O(n) *because* you can buy a faster computer.

No, O(6*n) is O(n) because that's what the notation means.
 
K

Keith Thompson

Bartc said:
I should have said '...less use to me'.


I'm not a mathematician. If there are people around who can understand this
stuff and it has useful applications then that's great.

The big O notation sounds useful if you're working with theory, but in the
practical world it's easy enough to measure this sort of thing.

Big O notation is what can tell you that your algorithm that's
blazingly fast on a list of 100 elements will take thousands of years
on a list of a million elements, while another algorithm that's half
the speed on 100 elements will finish in a few minutes on a million
elements.

Yes, you can measure this kind of thing, but an understanding of
computational complexity can be vital to understanding the results of
the measurements.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Richard Heathfield said:
Keith Thompson said:
Bartc said:
<snip>

And I'm guessing my code was O(log2 n) because it worked in binary.
Would this make it faster or slower than O(log n) or is there no
difference?

Big-O notation is a representation of computational complexity, not
runtime. The two are related, but not identical.

Let's say we have an O(6*n) algorithm. You can easily turn this into
an O(n) algorithm by buying a computer that works six times quicker
than the old one.
[...]

You can also do so without buying a faster computer.
O(6*n) *is* O(n).

O(6*n) *is* O(n) *because* you can buy a faster computer.

No, O(6*n) is O(n) because that's what the notation means.

And why does it mean that? Because you can buy a faster computer, that's
why.

It's also known as "Bachmann Landau notation". The Wikipedia article,
<http://en.wikipedia.org/wiki/Big-O_notation>, cites an article
published by Bachmann in 1894, and one published by Landau in 1909.

(Insert standard disclaimer about the reliability of Wikipedia, but I
think it tends to be fairly good on technical matters like this.)
 
R

robertwessel2

The use, here, is to recognise that repeated subtraction is O(n), but there
are division methods that are *better* than O(n). I think I'm right in
saying that normal long division is O(log n).


When talking about arithmetic, we usually talk about the sizes of the
number in digit of some base. Often base two, but commonly bignum
packages work in bases like 2**32. Which of course doesn't really
matter to the complexity, since it just turns into a constant factor.

Anyway, division by repeated subtraction is O(2**n). Division by the
schoolbook method is O(n**2). The fastest ways to divide involve an
approximation and several iteration of something like Newton-Raphson,
so are O((log n) * M), where M is the complexity of multiplication,
which can be as fast as O(n * (log n) * log(log n)), for SSA (a type
of FFT transform).

Flipping that around to multiplication for a moment, since there are
fewer special cases, shows how big-O can be helpful in picking an
algorithm.

For very small multiplications, repeated addition is the fastest
route. Just look at the code any compiler generates when multiplying
by two or three.

Up to few dozen digits, the school book method, perhaps with some
minor optimizations like delayed carry propagation is your best bet.
OTOH, it will be slower than repeated additions for very small
numbers.

Up to a few thousand digits, Karatsuba (O(n**1.6)), is a good bet,
but again, the increased overhead will make it slower than schoolbook
on shorter numbers.

Above that, the FFT based algorithms are your best bet, despite having
a very high constant factor, which makes them wholly unsuitable for
shorter numbers.
 
B

Ben Bacarisse

Richard Heathfield said:
Bartc said:



Big-O notation is a representation of computational complexity, not
runtime. The two are related, but not identical.

Let's say we have an O(6*n) algorithm. You can easily turn this into an
O(n) algorithm by buying a computer that works six times quicker than the
old one. You can even go to your vendor and say "we currently have a Model
X, but now we need something 6 times quicker than a Model X". He gives you
a Model Y, you give him much money, and suddenly you have O(n). So the
only real difference between O(6*n) and O(n) is a bit of cash,
right?

My main point is below but, for the record, O(6*n) = O(n) using = in
its strict mathematical sense. I think it is slightly misleading to
suggest any difference between them (i.e. "the only real difference"
is none at all).
But if we have an O(n*n) algorithm, we hit a problem.

and we also hit a terminology problem. Most people use O(...) when
they really want to say Theta(...). Merge sort is "an O(n*n)
algorithm" in a very precise sense. The number of comparisons
performed by a merge sort is a function MS(n) which is in O(n*n) just
as the number of comparisons done by bubble sort, BS(n), is in O(n*n).
Of course, MS(n) is also in O(n*log(n)) which BS(n) is not.

We should all get in the habit of saying "a Theta(n*n) algorithm" (if
it is true). Saying that something is O(n*n) is just a way of saying
that it's metric (time, space, no of comparisons, whatever) is no
"worse" than any other O(n*n) function, but it may be whole lot
better and big-O can't tell us that.

<for anyone who still cares>
O(n*n) is a set of functions, nothing more nothing less. Every
function, f, in O(n*n) has the property that one can find a C > 0 and
an x0 such that for all x > x0, abs(f(x)) < C*x*x.

Saying that a function f is in Theta(n*n) means that, eventually, its
absolute value is bounded both above and below by a positive multiple
of n*n. I.e. that we can find C1 and C2 > 0 and an x0 such that for
all x > x0, C1*x*x < abs(f(x)) < C2*x*x.
 
J

Johannes Bauer

Ben said:
and we also hit a terminology problem. Most people use O(...) when
they really want to say Theta(...).

Very true! This is IMHO very annoying, especially since many people
cannot tell apart o from O from theta from Theta.

Regards,
Johannes
 
S

Sri Harsha Dandibhotla

Oops, wrong. I meant:

TAOCP, Volume 2, Chapter 4.3.1, Algorithm D (page 272)

Regards,
Johannes

I do not have TAOCP.
Can you quote the algorithm instead?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top