Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Integer Length Count
Replies: 3   Last Post: Nov 24, 2012 2:41 AM

 Messages: [ Previous | Next ]
 Helen Read Posts: 145 Registered: 9/27/10
Re: Integer Length Count
Posted: Nov 24, 2012 2:41 AM

On 11/23/2012 3:30 AM, sylviehobbs@comcast.net wrote:

> I use a function in SAS that takes, in my case, a billion different
> integers of varying length and counts the frequency of the length of
> each integer by converting the digits in the integers into alpha
> characters and counting the frequency of the right most position of the
> alpha character.
>
> I am trying to do more and more routine programming in Mathematica
> and after poking around in documentation for the past 4 hours have yet to
> find the parallel function in Mathematica for counting the integer
> frequency length in a big data set. My family wants me to drop
> everything and put the Turkey in the oven. My cat Sylvester even fell
> asleep on his back in front of the refrigerator with his little legs up
> in the air, so he want miss the transfer of the Turkey from the stove to
> the refrigerator. OH WELL -- IT'S SHOW TIME!
>
> Meanwhile, let me know any recommendations you have on a Mathematica
> function that parallels the SAS Function.

I'm not entirely certain what you are trying to do here (a small example
would help), but if I understand you correctly, have a look at
IntegerLength.

From the Documentation:

IntegerLength[n]
gives the number of digits in the base 10 representation of the integer n.

threads over lists. It ignores the sign of the integer.
IntegerLength[0], by the way, is 0.

Here's a small example, starting with a list of 25000 random integers in
the range from -10000 to 10000.

list = RandomInteger[{-10000, 10000}, 25000];

lengths = IntegerLength[list]; (* getting the lengths of all the
integers in the list *)

maxlength = Max[lengths]; (* find the maximum length if not already
known *)

Now use Count to find the frequency of each length up to the maxlength.

Count[lengths,0]
Count[lengths,1]
and so on.

To get them all at once, Map it across the range from 0 to maxlength.

Map[Count[lengths, #] &, Range[0, maxlength]]

Or better yet, generate a list of ordered pairs {n, frequency} like so.

Map[{#, Count[lengths, #]} &, Range[0, maxlength]]

--
University of Vermont

Date Subject Author
11/23/12 sylviehobbs@comcast.net
11/24/12 daniel.lichtblau0@gmail.com
11/24/12 Ray Koopman