欧拉计划 发表于 2016-8-23 17:17:26

题目130:求合数n,使得n-1能够被能整除n的最小循环整数的长度整除

Composites with prime repunit property

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 − 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).

题目:

如果一个数全部由 1 组成,称之为一个循环整数。定义 R(k) 为长度为k的循环整数,例如 R(6) = 111111。

已知 n 是正整数以及 GCD(n, 10) = 1,可以证明,总存在一个 k 值使得 R(k) 可以被 n 整除,用 A(n) 表示这些 k 值中最小的,例如 A(7) = 6,A(41) =5。

已知对于所有大于 5 的质数 p,p-1 能够被 A(p) 整除。例如,当 p=41 时,A(41)=5,40 可以被 5 整除。

但是,有一些罕见的合数也满足整个条件;前五个例子是 91, 259, 451, 481 和 703。

求前 25 个满足 GCD(n, 10) = 1 以及 n-1 能被 A(n) 整除的合数 n 之和。

jerryxjr1220 发表于 2017-7-19 14:32:13

from math import gcd
def A(n):
        if gcd(n, 10) != 1: return 0
        x, k = 1, 1
        while x != 0:
                x = (x*10+1) % n
                k += 1
        return k
primes = *1000000
primes[:2] = *2
for i,prime in enumerate(primes):
        if prime:
                for j in range(i*i, 1000000, i):
                        primes = False
count = 0
s = 0
i = 2
while True:
        if not primes and gcd(i, 10) == 1 and (i-1)%A(i)==0:
                count += 1
                s += i
        if count == 25:
                break
        i += 1
print(s)
149253
页: [1]
查看完整版本: 题目130:求合数n,使得n-1能够被能整除n的最小循环整数的长度整除