Results 1 to 2 of 2

Thread: Java --> timing factorials

  1. #1
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152

    Java --> timing factorials

    I forgot about Java.

    But, it's still here.

    Here are some factorial timings.

    It was run using Java 1.6, from within NetBeans 7.0.

    (I agree, the times are strange.)

    ' code -------------------------------------------------------------------------------------------------
    
    package fact;
    
    import java.math.BigInteger;
    
    public class Fact {
    
        public static BigInteger factorial(int n) {
            BigInteger product;
            int i;
            product = BigInteger.valueOf(1);
            for (i = 2; i <= n; i++) {
                product = product.multiply(BigInteger.valueOf(i));
            }
            return product;
        }
    
        public static void main(String[] args) {
            BigInteger value;
            long start, stop;
            double seconds;
            int i, j;
            System.out.println("n!     seconds");
            for (i = 1; i <= 20; i++) {
                j = i * 10000;
                start = System.currentTimeMillis();
                value = factorial(j);
                stop = System.currentTimeMillis();
                seconds = (stop - start) / 1000.0;
                System.out.printf("%06d %07.3f\n", j, seconds);
            }
        }
    }
    
    ' output -----------------------------------------------------------------------------------------------
    
    n!     seconds
    010000 000.768
    020000 003.198
    030000 007.302
    040000 013.405
    050000 021.158
    060000 032.471
    070000 044.814
    080000 059.941
    090000 075.980
    100000 093.814
    110000 116.617
    120000 136.939
    130000 159.905
    140000 186.705
    150000 313.046
    160000 292.847
    170000 282.498
    180000 325.723
    190000 359.425
    200000 397.941
    
    Last edited by danbaron; 22-12-2011 at 12:15.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Very interesting in comparison with the Python, which performed better on this case...


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Python 3 --> Timing factorials.
    By danbaron in forum Scripting
    Replies: 4
    Last Post: 19-12-2011, 11:26
  2. thinBasic --> Java or Python
    By danbaron in forum Development
    Replies: 9
    Last Post: 19-12-2011, 07:08
  3. Lisp: Timing Recursive Factorials
    By danbaron in forum Other languages
    Replies: 0
    Last Post: 18-03-2011, 06:29
  4. Java --> Timing a Matrix Inversion
    By danbaron in forum Scripting
    Replies: 2
    Last Post: 07-08-2010, 10:48
  5. C --> Timing a Matrix Inversion
    By danbaron in forum C / C++
    Replies: 16
    Last Post: 05-08-2010, 10:30

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •