Created
February 11, 2012 07:36
-
-
Save fictorial/1797559 to your computer and use it in GitHub Desktop.
embedly challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem 1 | |
* | |
* See http://apply.embed.ly/1 | |
* | |
* The trick here is to realize that primitive types will overflow very quickly. | |
* This problem gave me a nice excuse to learn libgmp. | |
* | |
* On Mac OS X, | |
* $ brew install gmp | |
* $ gcc problem1.c && ./a.out | |
* | |
* Brian Hammond, Sat Feb 11 00:17:55 EST 2012 | |
* [email protected] | |
*/ | |
#include <stdio.h> | |
#include <gmp.h> | |
void F(mpz_t out, unsigned long int n) { | |
mpz_init_set_ui(out,n); | |
while (n > 1) | |
mpz_mul_ui(out,out,--n); | |
} | |
void R(mpz_t out, mpz_t f) { | |
mpz_init(out); | |
while (mpz_sgn(f) != 0) { | |
mpz_t t; | |
mpz_init(t); | |
mpz_mod_ui(t,f,10); | |
mpz_add(out,out,t); | |
mpz_tdiv_q_ui(f,f,10); | |
mpz_clear(t); | |
} | |
} | |
unsigned long int find_8001() { | |
unsigned long int i=1; | |
mpz_t f, r; | |
mpz_init(f); | |
mpz_init(r); | |
while (i++) { | |
F(f,i); | |
R(r,f); | |
if (mpz_cmp_ui(r,8001) == 0) break; | |
} | |
mpz_clear(f); | |
mpz_clear(r); | |
return i; | |
} | |
int main(int argc, char *argv[]) { | |
printf("lowest value of n where R(n) = 8001 is %lu\n", find_8001()); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# See http://apply.embed.ly/2 | |
use strict; | |
use warnings; | |
use utf8; | |
my (%h, @samples); | |
while (<DATA>) { | |
next unless /^(\s+)<p>/; | |
$h{length $1}++; # cheating or clever? idk but exploiting data format regularity is ok by me | |
} | |
my $i=1; | |
foreach my $k (sort {$a<=>$b} keys %h) { | |
push @samples, $i for 0..$h{$k}; | |
++$i; | |
} | |
sub mean { | |
my (@a) = @_; | |
my $s; | |
$s += $_ for @a; | |
$s /= @a; | |
} | |
my $m = mean(@samples); | |
my $sd = sqrt mean(map { ($_ - $m)**2 } @samples); | |
printf "%.1f\n", $sd; | |
__DATA__ | |
<html> | |
<head> | |
<title>Embedly Apply Document</title> | |
</head> | |
<body> | |
<article> | |
<p>ncetta. Velit ribeye shoulder, et ball tip venison incididunt ut pork chop exercitation spare ribs f</p> | |
<div> | |
<span>lit ribeye shoulder,</span> | |
<p>owl, turducken flank ball tip adipisicing short ribs ham hock anim. Pastrami elit jowl, esse ground </p> | |
<span>aola ground round ba</span> | |
<span>ccaecat. Adipisicing</span> | |
</div> | |
<span> aliqua occaecat. Ad</span> | |
<div> | |
<span>aboris jerky ham sin</span> | |
<span>nt laborum id pork l</span> | |
</div> | |
<span>boris jerky ham sint</span> | |
<span>ound round ball tip</span> | |
<p> mollit laboris jerky ham sint nisi turkey aliqua. Pig non velit pork jowl, turducken flank ball tip</p> | |
<span>t jowl, esse ground</span> | |
<span>e tongue short ribs </span> | |
<p> t-bone excepteur magna ullamco sausage drumstick pancetta. Velit ribeye shoulder, et ball tip venis</p> | |
<p>ncetta incididunt nulla. Quis in t-bone excepteur magna ullamco sausage drumstick pancetta. Velit ri</p> | |
<p>is jerky ham sint nisi turkey aliqua. Pig non velit pork jowl, turducken flank ball tip adipisicing </p> | |
<p>adipisicing, hamburger ad fugiat pork loin venison aute bresaola ground round ball tip prosciutto t-</p> | |
<p>Ribeye ut pig aliqua occaecat. Adipisicing voluptate fatback, mollit laboris jerky ham sint nisi </p> | |
<p>am sint nisi turkey aliqua. Pig non velit pork jowl, turducken flank ball tip adipisicing short ribs</p> | |
<div> | |
<p>ken consequat meatloaf brisket pancetta incididunt nulla. Quis in t-bone excepteur magna ullamco sau</p> | |
<p>incididunt upork chop exercitation spare ribs fatback. Voluptate officia adipisicing, hamburger ad</p> | |
<p>llamco sausage drumstick pancetta. Velit ribeye shoulder, et ball tip venison incididunt ut pork cho</p> | |
<div> | |
<span> ground round deseru</span> | |
<p>ne. Swine consequat ex reprehenderit. Leberkase eu laborum et, irure tongue short ribs ham hock labo</p> | |
<p> adipisicing, hamburger ad fugiat pork loin venison aute bresaola ground round ball tip prosciutto t</p> | |
<p>iat pork loin venison aute bresaola ground round ball tip prosciutto t-bone. Swine consequat ex repr</p> | |
<div> | |
<p>is jerky ham sint nisi turkey aliqua. Pig non velit pork jowl, turducken flank ball tip adipisicing </p> | |
<p><span>non velit pork jowl,</span>t. Leberkase eu laborum et,irure tongue short ribs ham hest esse.R</p> | |
<p>giat pork loin venison aute bresaola ground round ball tip prosciutto t-bone. Swine consequat ex rep</p> | |
<p> prosciutto est esse. Ribeye ut pig aliqua occaecat. Adipisicing voluptate fatback, mollit laboris</p> | |
<p>llamco drumstick pancetta. Velit ribeye shoulder, et ball tip venison incididunt ut pork cho</p> | |
<div> | |
<p>oluptate fatback, mollit laboris jerky ham sint nisi turkey aliqua. Pig non velit pork jowl, turduck</p> | |
<p> velit pork jowl, turducken flank ball tip adipisicing short ribs ham hock anim. Pastrami elit jowl,</p> | |
<p>ipisicing short ribs ham hock anim. Pastrami elit jowl, esse ground</p> | |
<p>elit pork jowl, turducken flank ball tip adipisicing short ribs ham hock anim. Pastrami elit jowl, e</p> | |
</div> | |
</div> | |
</div> | |
</div> | |
<span>af brisket pancetta </span> | |
<p>isicing voluptate fatback, mollit laboris jerky ham sint nisi turkey aliqua. Pig non velit pork jowl</p> | |
<p>pancetta incididunt nulla. Quis in t-bone excepteur magna ullamco sausage drumstick pancetta. Velit</p> | |
<p>nisi turkey aliqua. Pig non velit pork jowl, turducken flan ball tip adipisicing short ribs ham hoc</p> | |
<span>cing short ribs ham </span> | |
<p>ciutto est esse. Ribeye ut pig aliqua occaecat. Adipisicing voluptate fatback, mollit laboris jerky</p> | |
<p>rehenderit. Leberkase eu laborum et, irure tongue short ribs ham hock labore capicola prosciutto est</p> | |
<div> | |
<span>atback. Voluptate of</span> | |
<p>nsequat ex reprehenderit. Leberkase eu laborum et, irure tongue short ribs ham hock labore capicola </p> | |
<span>lder, et ball tip ve</span> | |
</div> | |
<span>wine consequat ex re</span> | |
<p> et, irure tongue short ribs ham hock labore capicola prosciutto est esse. Ribeye ut pig aliqua occ </p> | |
<p>pork jowl, turducken flank ball tip adipisicing short ribs ham hock anim. Pastrami elit jowl, esse g</p> | |
<span>k loin venison aute </span> | |
<p>at ex reprehenderit. Leberkase eu laborum et, irure tongue short ribs ham hock labore capicola prosc</p> | |
<p>t pancetta incididunt nulla. Quis in t-bone excepteur magna ullamco sausage drumstick pancetta. Veli</p> | |
<div> | |
<p>ound round ball tip prosciutto t-bone. Swine consequat ex reprehenderit. Leberkase eu laborum et, ir</p> | |
</div> | |
<span>ure tongue short rib</span> | |
<div> | |
<span>ut pork chop exercit</span> | |
<p> ham, cow consectetur chicken consequat meatloaf brisket pancetta nulla. Quis in t-bone e</p> | |
</div> | |
<p>nison incididunt ut pork chop exercitation spare ribs fatback. Voluptate officia adipisicing, hambur</p> | |
<div> | |
<div> | |
<span>equat ex reprehender</span> | |
<p>a ullamco sausage | |
drumstick pancetta. Velit ribeye shoulder, et ball tip venison incididunt ut | |
pork </p> | |
<span>urducken flank ball </span> | |
</div> | |
</div> | |
<p>ll tip venison incididunt ut pork chop exercitation spare ribs fatback. Voluptate officia adipisicin</p> | |
</article> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# See http://apply.embed.ly/3 | |
@hz = map { int((2520/$_) + 0.5) } 1..900; | |
$sum += $_ for @hz; | |
foreach (@hz) { | |
$i += $_; | |
$n++; | |
last if $i >= int($sum/2 + 0.5); | |
} | |
print "$n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment