Results 1 to 3 of 3

Thread: Hey kenshi!

Hybrid View

  1. #1
    Guest

    Hey kenshi!

    Remember your assembly program that could factor one quadrillion in 6 seconds? I made a O'Caml program. On my machine it takes 12 seconds (better than Ruby and Python who took a couple of minutes). Try it out.

    to compile:
    Code:
    % ocamlopt nums.cmxa factor.ml -o factor
    And here's the actual code:
    Code:
    open Big_int;;
    
    let rec get_factors n sqroot diviser =
    
     if (le_big_int diviser sqroot) then begin
      if (eq_big_int (mod_big_int n diviser) zero_big_int) then begin
       Printf.printf "%s and %s\n" (string_of_big_int diviser) (string_of_big_int
       (div_big_int n diviser));
       flush stdout;
      end;
      get_factors n sqroot (add_big_int diviser unit_big_int);
     end
    
    let _ = 
     if (Array.length Sys.argv) < 2 then begin
      Printf.printf "Usage: %s <Integer>\n" Sys.argv.(0);
      exit 1;
     end;
     
     let n = big_int_of_string Sys.argv.(1) in
     let sqroot = sqrt_big_int n in
     get_factors n sqroot unit_big_int
    I should try with 64-bits integers to see if I get a speed improvement.

  2. #2
    Guest

    Re: Hey kenshi!

    Here's something much faster, but more limited too:
    Code:
    open Int64
    
    let rec get_factors n sqroot diviser =
    
     if diviser < sqroot then begin
      if (rem n diviser) = zero then begin
       Printf.printf "%s and %s\n" (to_string diviser) (to_string (div n
       diviser));
       flush stdout
      end;
      get_factors n sqroot (succ diviser)
     end
    
    let _ = 
     if (Array.length Sys.argv) < 2 then begin
      Printf.printf "Usage: %s <Integer>\n" Sys.argv.(0);
      exit 1
     end;
     
     let n = of_string Sys.argv.(1) in 
     let sqroot = of_float(sqrt(to_float(n))) in
     get_factors n sqroot one
    Compile with:
    Code:
    % ocamlopt factor2.ml -o factor
    And by the way, I was mistaken, my first program takes much more than 12 seconds.

  3. #3

    Re: Hey kenshi!

    I tried compiling it but it just gave me an extremely long (longer than a page) list of options as if I had used the command wrong. I might try to figure out what's wrong later.

Similar Threads

  1. Hey Kenshi
    By kenshi in forum General Chat
    Replies: 12
    Last Post: 08-27-2002, 07:41 AM
  2. Hey Kenshi!
    By ndogg in forum General Chat
    Replies: 17
    Last Post: 01-11-2002, 03:44 AM
  3. Kenshi
    By in forum General Chat
    Replies: 7
    Last Post: 12-27-2001, 05:43 AM
  4. Kenshi
    By pbharris in forum General Chat
    Replies: 3
    Last Post: 12-23-2001, 11:40 PM
  5. Hey kenshi
    By in forum General Chat
    Replies: 8
    Last Post: 12-10-2001, 09:45 PM

Bookmarks

Posting Permissions

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