comp.lang.ada
 help / color / mirror / Atom feed
* Re: Factorial Base - e
       [not found] ` <7ac66f$oen$2@sun500.nas.nasa.gov>
@ 1999-02-18  0:00   ` nabbasi
  0 siblings, 0 replies; only message in thread
From: nabbasi @ 1999-02-18  0:00 UTC (permalink / raw)


In article <7ac66f$oen$2@sun500.nas.nasa.gov>, hook@nas.nasa.gov says...
>
 
>  I'm not familiar with this program, but I _do_ have an implementation
>  of the "spigot" algorithm for computing the decimal representation of
>  e that sounds like much the same thing. I first learned about this
>  approach from an article in the American Mathematical Monthly, which
>  is (sort of) referenced in the comments given in the source code that
>  I include below.
>
 
--
-- This is an Ada implementation of decimal representation of 'e'
-- based on SPIGOT algorithm for \pi by
-- S.  Rabinowitz & S. Wagon, _The_American_Mathematical_Monthly_, March 1995
--
--  A C implementation of the above was posted on the net by
--  Ed Hook
--  MRJ Technology Solutions, Inc.
--  NAS, NASA Ames Research Center
--  Internet: hook@nas.nasa.gov
--  
-- This is an Ada implementation of the Ed Hook program but using GNAT 
-- (gnu Ada compiler), with the added small feature is that it 
-- computes the frequency of each digit in e.
--
-- the following table is the result. my PC is still running trying to find the
-- frequency for 200,000 digits and more for e, and it's been several days
-- and not finished. So this is a partial results. (PC is 200 MHz pentium,
-- running Linux 2.0.36, and compiler is GNAT 3.11p
--
-- offcourse as number of digits of e goes very large, each digit is expected
-- to show as often as any other digit.
--
-- Nasser Abbasi  nabbasi@pacbell.net
--
-- results:

-- this is distributions of digits in e as function of number of digits.
-- for example, when looking at 5000 digits of e, we find 497 0's,
-- 478 1's, etc..  (this is for digits after the decimal point of e)
--
--
--                                   #digits  in e
--            --------------------------------------------------------------
--               500  5,000  20,000  50,000  200,000  1,000,000   5,000,000
--            ---------------------------------------------------------------
--how many 0's   51    497    1,949  4,948
--how many 1's   43    478    2,010  5,055
--how many 2's   50    492    2,020  4,969
--how many 3's   53    514    2,080  5,026     STILL IN PROGRESS FOR 3 DAYS
--how many 4's   52    470    1,989  4,966     NEED TO BOOT PC :(
--how many 5's   44    478    1,979  5,046
--how many 6's   51    545    2,057  5,133
--how many 7's   60    525    1,977  4,959
--how many 8's   40    509    1,966  4,972
--how many 9's   56    492    1,974  4,926
--
------------------------------------------------------------------------
--most occuring  '7'   '7'     '3'    '6'
------------------------------------------------------------------------
--least occuring '8'   '4'     '0'    '9'
------------------------------------------------------------------------
--difference
--between largest 20   55      131    207
--and smallest
--in frequency
------------------------------------------------------------------------
--difference
--between largest 4%  1.1%   0.655%  0.414%
--and smallest
--frequency in %
--
--
--Compiler:    GNAT 3.11p , see http://www.adahome.com to download
--To compile:  save this file as dist_e_final.adb and type
--             gnatmake dist_e_final.adb
--system:      Linux 2.0.36
--Date:        feb. 17, 1999
--To Run:      ./dist_e_final  <number_of_digits>
--             For example, to see e for 70 digits do:
--
-- /home/nabbasi/my_ada $./dist_e_final 70
-- 2.7182818284590452353602874713526624977572470936999595749669676277240766
-- frequency of  0 is  4
-- frequency of  1 is  3
-- frequency of  2 is  9
-- frequency of  3 is  4
-- frequency of  4 is  7
-- frequency of  5 is  7
-- frequency of  6 is  10
-- frequency of  7 is  12
-- frequency of  8 is  5
-- frequency of  9 is  9
--


with Ada.Text_Io;         use Ada.Text_Io;
with ada.command_line;    use ada.command_line;

procedure Dist_E_final is
   type E_Type is array( Natural range <> ) of Natural;
   Dist : array(0..9) of Natural := (others => 0);
   Num_Of_Digits : Natural;
begin

   if( Argument_Count /= 1 ) then
      Put_Line("usage: dist_e <number_of_digits>");  return;
   end if;

   begin
      Num_Of_Digits := natural'value( Argument(1));

      if( Num_Of_Digits = 0 ) then
         Put_Line("value for number of digits must be larger than zero"); 
         return;
      end if;

   exception
      when others =>
         Put_Line("Exception. invalid value for number of digits");  
         return;
   end;


   declare                          -- the algorithm itself is in this block
      E: E_Type( 1 .. Num_Of_Digits+2 ) := (others=> 1);
      Carry : Natural;
   begin

      Put("2.");

      for I in E'first .. E'Last-2 loop
         Carry := 0;
         for J in reverse E'first .. E'Last loop
            E(J) := E(J)*10;
            E(J) := E(J)+Carry;
            Carry := E(J)/(J+1);
            E(J) := E(J) rem (J+1);
         end loop;

         Put(Natural'Image(Carry)(2));     -- print current digit of e
         Dist(Carry) := Dist(Carry) + 1;   -- update this digit frequency
      end loop;
   end;

   New_Line;
   for I in Dist'Range loop
      Put_line("frequency of " & Natural'Image(I) & " is "
               & Natural'Image( Dist(I)));
   end loop;

end Dist_E_final;




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1999-02-18  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <19990215100437.15893.00002176@ng-fp1.aol.com>
     [not found] ` <7ac66f$oen$2@sun500.nas.nasa.gov>
1999-02-18  0:00   ` Factorial Base - e nabbasi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox