Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dc706a
commit 34e8f57
Showing
2 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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