code

Project Euler Problem 47

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/python
  2.  
  3. import pysieve
  4.  
  5. def p47(n):
  6.     while 1:
  7.         ints = range(n, n+4)
  8.         if len(list(set(pysieve.factor(ints[0])))) == 4 and \
  9.            len(list(set(pysieve.factor(ints[1])))) == 4 and \
  10.            len(list(set(pysieve.factor(ints[2])))) == 4 and \
  11.            len(list(set(pysieve.factor(ints[3])))) == 4:
  12.             return ints
  13.         n += 1
  14.  
  15.        
  16. print p47(100000)

Project Euler Problem 50

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/python
  2.  
  3. import Crypto.Util.number
  4.  
  5. def primes(n):
  6.   """ returns a list of prime numbers from 2 to < n """
  7.   if n < 2:  return []
  8.   if n == 2: return [2]
  9.   # do only odd numbers starting at 3
  10.   s = range(3, n, 2)
  11.   # n**0.5 may be slightly faster than math.sqrt(n)
  12.   mroot = n ** 0.5
  13.   half = len(s)
  14.   i = 0
  15.   m = 3
  16.   while m <= mroot:
  17.     if s[i]:
  18.       j = (m * m - 3)//2
  19.       s[j] = 0
  20.       while j < half:

Project Euler Problem 33

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/python
  2.  
  3. for n in range(10, 100):
  4.     if n % 10 != 0:
  5.         for d in range(10, 100):
  6.             if d % 10 != 0 and d != n:
  7.                 s_n = str(n)
  8.                 s_d = str(d)
  9.                 n_n = 0
  10.                 n_d = 0
  11.                 if s_n[0] == s_n[1] or s_d[0] == s_d[1]:
  12.                     continue
  13.                 if s_n[0] == s_d[0]:
  14.                     n_n = s_n[1]
  15.                     n_d = s_d[1]

Project Euler Problem 56

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/python
  2.  
  3. max_sum = 0
  4. for a in range(90, 100):
  5.     for b in range(90, 100):
  6.         n = a**b
  7.         my_sum = sum(int(d) for d in str(a**b))
  8.         if my_sum > max_sum:
  9.             max_sum = my_sum
  10.  
  11. print max_sum

Project Euler Problem 53

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/python
  2.  
  3. import math
  4.  
  5. memo = {}
  6.  
  7. def c(n, r):
  8.     if not n in memo:
  9.         memo[n] = math.factorial(n)
  10.    
  11.     if not r in memo:
  12.         memo[r] = math.factorial(r)
  13.  
  14.     if not n-r in memo:
  15.         memo[n-r] = math.factorial(n-r)
  16.  
  17.     n_r = memo[n-r]
  18.     n   = memo[n]
  19.     r   = memo[r]
  20.     return n/(r*(n_r))
  21.  
  22. count = 0
  23. for n in range(23, 101):
  24.     for r in range(1, n):
  25.         if c(n, r) > 1000000:

Project Euler Problem 97

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl -w
  2.  
  3. sub foo {
  4.     my $num = 1;
  5.     for(my $i = 1; $i <= 7830457; $i++) {
  6.         $num = $num * 2 % 10000000000;
  7.     }
  8.     return $num;
  9. }
  10.  
  11. print 28434*foo() % 10000000000;
  12. print "\n";

Project Euler Problem 55

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl
  2.  
  3. sub p55 {
  4.     my $count = 0;
  5.     for(my $i = 1; $i < 10000; $i++) {
  6.         $count++ if(is_ly($i));
  7.     }
  8.     return $count;
  9. }
  10.  
  11. sub is_ly {
  12.     my $n = shift;
  13.     my @stack;
  14.     push(@stack, $n+reverse($n));
  15.     while(@stack < 50) {
  16.         $n = $stack[@stack-1];
  17.         my $rn = reverse($n);
  18.         return 0 if($n eq $rn);
  19.         push(@stack, $n+$rn);
  20.     }
  21.     return 1;
  22. }
  23.  
  24. my $count = p55();

Project Euler Problem 38

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl -w
  2.  
  3. sub is_pandigital {
  4.     my $n = shift;
  5.     my $seen;
  6.     my @digits = split(//, $n);
  7.     foreach my $d (@digits) {
  8.         return 0 if($seen->{$d});
  9.         return 0 if($d eq "0");
  10.         $seen->{$d} = 1;
  11.     }
  12.     return 1;
  13. }
  14.  
  15. sub p38 {
  16.     my $len = 0;
  17.     my $max = 0;
  18.     for(my $k = 100000; $k > 0; $k--) {
  19.         my $prod    ='';
  20.         my $n       = 1;
  21.         while(length($prod) <= 9) {

Project Euler Problem 37

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl -w
  2.  
  3. use Math::Prime::XS qw(is_prime primes);
  4. use Data::Dumper;

Project Euler Problem 32

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. my $prods;
  6. for(my $i = 0; $i < 2000; $i++) {
  7.     my $j;
  8.     my $j_max;
  9.     ($j, $j_max) = (10, 50) if($i <= 1000);
  10.     ($j, $j_max) = (1, 5)   if($i > 1000);
  11.     for(; $j < $j_max; $j++) {
  12.         my $p = $i*$j;
  13.         if(is_pandigital("$i$j$p")) {
  14.             if(!$prods->{$p}) {
  15.                 $prods->{$p} = 1;
  16.                 print "($i, $j, $p) was pandigital\n";
  17.             }
  18.         }
  19.     }

Syndicate content