Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I tried PDL, but it was also very slow (about 49 minutes):
use strict; use PDL::LiteF; use PDL::Primitive; use PDL::Reduce; use PDL::Types; use integer; my ($ans, $total, $total2, $sum, $num, $ones, $zeros); my $shifts = long ( 0, 1, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7, 1 << 8, 1 << 9, 1 << 10, 1 << 11, 1 << 12, 1 << 13, 1 << 14, 1 << 15, 1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20, 1 << 21, 1 << 22, 1 << 23 ); $ans = 0; for my $num (0 .. 16777216) { $sum = sum(which($shifts & $num)); $total = 2*$sum - 300; $ans++ if $total == 0; } print "$ans\n";
I did much better with Inline::C (about 4 seconds after the first time):
use Inline C; print "answer is ", bitlevels(), "\n"; __END__ __C__ /* begin c++ code */ /* #include<iostream> */ /* using namespace std; */ int bitlevels() { unsigned int ans=0; int num, total, i; unsigned int shift; for(num=0;num<16777216;++num) { total=0; shift=1; for(i=1;i<=24;++i) { if(num & shift) { total+=i; } else { total-=i; } shift<<=1; } if(total==0) ++ans; } return ans; } /* end c++ code */

In reply to Re: Believably slow.. by tall_man
in thread Unbelievably slow.. by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found