Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Finding Primes

by Foggy Bottoms (Monk)
on Aug 14, 2003 at 17:05 UTC ( [id://283938]=note: print w/replies, xml ) Need Help??


in reply to Re: Finding Primes
in thread Finding Primes

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 !!!

Replies are listed 'Best First'.
Re: Re: Re: Finding Primes
by simonm (Vicar) on Aug 14, 2003 at 18:21 UTC
    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?

Re: Re: Re: Finding Primes
by Anonymous Monk on Aug 14, 2003 at 22:40 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://283938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found