// you’re reading...

Project Euler Solutions

Project Euler Problem 130 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

A number consisting entirely of ones is called a repunit. We shall define R(k) to be a repunit of length k; for example, R(6) = 111111.

Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least such value of k; for example, A(7) = 6 and A(41) = 5.

You are given that for all primes, p > 5, that p minus 1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5.

However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703.

Find the sum of the first twenty-five composite values of n for which
GCD(n, 10) = 1 and n − 1 is divisible by A(n).

Analysis

We started with the given information and added to it by simply checking which composite values of n-1 were divisible by A(n). The problems 129, 130, 132 and 133 were all solved with recreation in mind so not a lot of thought transpired. Compared to some solutions these do appear to be slower but simpler to understand.

Solution

Runs < 3 seconds in Python.

from Euler import is_prime, gcd
 
def A(n):
  if is_prime(n) or gcd(n, 10) != 1: return None
  x, k = 1, 1
  while x != 0:
    x = (x*10+1) % n
    k += 1
  return k
 
s = [91, 259, 451, 481, 703]
n = s[-1]
while len(s)< 25:
  n += 2
  an = A(n)
  if an != None and (n-1) % an == 0: 
    s.append(n)
 
print "Answer to PE130 = ", sum(s)

Comments

More information on the Euler module can be found on the tools page.

Discussion

No comments for “Project Euler Problem 130 Solution”

Post a comment