Skip to content

Commit

Permalink
problem 130
Browse files Browse the repository at this point in the history
  • Loading branch information
juanplopes committed Aug 6, 2012
1 parent 9dc706a commit 34e8f57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
38 changes: 38 additions & 0 deletions 130.boo
@@ -0,0 +1,38 @@
/*
Like 132:
R(k) can be writen as: (10^k-1) / (10-1).
Knowing if R(k) is divisible by some number n is same as knowing if:
(10^k-1)/9 mod n == 0
or
(10^k-1) mod (n*9) == 0
or
10^k mod n*9 == 1
We just need to know, then, which are the 25 first composite numbers for which
R(k) is divisible by k-1.
*/
import System
import System.Collections.Generic
import System.Linq.Enumerable

P = PrimeNumbers()

def pow(a as int, n as int, mod as int):
r, p = (1L, cast(long, a))
while(n):
if n%2: r=(r*p)%mod
p=(p*p)%mod
n/=2

return r

def have_property(n as int):
return not P.IsPrime(n) and pow(10, n-1, n*9) == 1

answer = range(5, int.MaxValue).Where(have_property).Take(25).Sum()

print answer
assert answer == 149253
16 changes: 6 additions & 10 deletions 138.boo
Expand Up @@ -31,22 +31,18 @@ import System
import System.Linq.Enumerable
import System.Math


def abs(a as BigInteger, b as BigInteger) as (BigInteger):
return ((a, b) if a>=0 and b>=0 else (-a,-b))

px, py = BigInteger(0), BigInteger(1L)
lx, ly = BigInteger(0), BigInteger(-1L)

answer = 0L
for i in range(12):
px, py = -9*px -8*py -8, -10*px - 9*py -8
if px > 0 and py > 0:
answer += py
px, py = -9*px -8*py -8, -10*px - 9*py -8
if px > 0 and py > 0:
answer += py

lx, ly = -9*lx -8*ly +8, -10*lx - 9*ly +8
if lx > 0 and ly > 0:
answer += ly
lx, ly = -9*lx -8*ly +8, -10*lx - 9*ly +8
if lx > 0 and ly > 0:
answer += ly

print answer
assert answer == 1118049290473932L

0 comments on commit 34e8f57

Please sign in to comment.