Wrong-but-not-incorrect code

W

Walter Roberson

:(long division uses O(n^2) time or worse for large n),

And much *much* worse time when dividing by 0 ;-)
 
K

Keith Thompson

Chris Croughton said:
If you can divide 1639657633472406784 by 7 in your head reliably, go for
it! I can't, reliably, so I prefer rules which are simple and use O(n)
time (long division uses O(n^2) time or worse for large n), like the sum
of the digits modulo 3 == 0 for multiples of 3 (multiples of 2, 5 and 10
are even faster, being O(1)).

But you don't need to use long division when dividing by 7 (unless
you're using a base smaller than 7).
 
T

Tim Rentsch

Gorgeous. You can even do this:

while (condition) {
/* stuff */
} while (condition);

For bonus points, make sure that you leave the first loop with break -
at the precise moment that the condition is false.


<amusement>

For an increase in transmogrification factor, may I suggest using the
infamous "do while while" construction:

do while (condition) {
/* stuff */
} while (condition);

Include both 'break;' and 'continue;' statements in the loop body for
additional hilarity.

</amusement>
 
C

CBFalconer

Chris said:
If you can divide 1639657633472406784 by 7 in your head reliably,
go for it! I can't, reliably, so I prefer rules which are simple
and use O(n) time (long division uses O(n^2) time or worse for
large n), like the sum of the digits modulo 3 == 0 for multiples
of 3 (multiples of 2, 5 and 10 are even faster, being O(1)).

I can do the division by 7 much more easily and reliably than the
other method. The primary difference is you start the scan from
the left. I can discard everything except the remainder when
dividing a max 2 digit value by 7. I can always discard any
leftmost zeroes, and only need worry about dividends in the range 7
through 69.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top