in reply to Finding Primes

A quick question:

100 * 100 = 10000 (5 digits)
900 * 900 = 810000 (6 digits)

not all sets of numbers with the same mubers of digits will result in a product with exactly double the number of digits. So is exactly 400 digits the requirement?

Just for fun.
John

Replies are listed 'Best First'.
Re: Re: Finding Primes
by Foggy Bottoms (Monk) on Aug 14, 2003 at 17:05 UTC
    It's easy to determine how many digits you'll have in the resulting number.
    In your example : 100*100 is actually 102*102 hence 104 which turns out to be 5 digits. The rule, hence is :
    Take any number, divide repeatedly by ten until you reach a number greater than or equal to 1 and strictly lower than 10. The number of times you divided that number is actually the number of digits minus 1 that number has.
    Example : 84212 divided by 10 is 8421.2 (1)
    8421.2 divided by 10 is 842.12 (2)
    842.12 divided by 10 is 84.212 (3)
    84.212 divided by 10 is 8.4212 (4)
    Hence the number has 4+1=5 digits !!!
      It's easy to determine how many digits you'll have in the resulting number.

      I'm a bit puzzled: sure, logarithms let you replace multiplication with addition -- part of the magic of slide rules -- but isn't taking the base-10 log of something typically as much or more labor intensive then doing the multiplication?

      For example: 321 * 311 produces five digits, while 321 * 312 produces six digits.

      Sure, you could work out that 10 ** 2.5065 * 10 ** 2.493 = 10 ** 4.9995 and thus five digits, while 10 ** 2.5065 * 10 ** 2.494 = 10 ** 5.0005 and thus six, but is that really easier?

      Why not just use something like this:

      #!/usr/bin/perl -wl sub logb10 { return log(+shift)/log(10); } print int(logb10(100) + logb10(100) + 1); print int(logb10(321) + logb10(311) + 1); print int(logb10(321) + logb10(312) + 1); __DATA__ output: 5 5 6

      Or maybe we could just pull out the magic length method and stop trying to focus on a problem that isn't really a problem?

        It probably amounts to much the same thing, but there is a log10() function available as a part of POSIX which is in the standard distribution.

        use POSIX qw[log10]; print log10 312; 2.49415459401844

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.