Last active
June 11, 2019 11:16
-
-
Save ZAYEC77/fac62b4799f75189a98884eef1f54c05 to your computer and use it in GitHub Desktop.
N mod D = 0 when N contains only 0 or 1
This file contains hidden or 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
void main() { | |
print('D => N'); | |
var length = 3000; | |
var map = primesMap().take(length).toList(); | |
for (int d = 0; d < length; d++) { | |
var dd = map[d]; | |
BigInt n = getN(BigInt.from(dd)); | |
if (n == dd) { | |
print('prime here start ----'); | |
print('$dd => $n '); | |
print('prime here end -----'); | |
} else { | |
print('$dd => $n '); | |
} | |
} | |
} | |
Iterable<int> primesMap() { | |
Iterable<int> oddprms() sync* { | |
yield(3); yield(5); // need at least 2 for initialization | |
final Map<int, int> bpmap = {9: 6}; | |
final Iterator<int> bps = oddprms().iterator; | |
bps.moveNext(); bps.moveNext(); // skip past 3 to 5 | |
int bp = bps.current; | |
int n = bp; | |
int q = bp * bp; | |
while (true) { | |
n += 2; | |
while (n >= q || bpmap.containsKey(n)) { | |
if (n >= q) { | |
final int inc = bp << 1; | |
bpmap[bp * bp + inc] = inc; | |
bps.moveNext(); bp = bps.current; q = bp * bp; | |
} else { | |
final int inc = bpmap.remove(n); | |
int next = n + inc; | |
while (bpmap.containsKey(next)) { | |
next += inc; | |
} | |
bpmap[next] = inc; | |
} | |
n += 2; | |
} | |
yield(n); | |
} | |
} | |
return [2].followedBy(oddprms()); | |
} | |
BigInt getN(BigInt d) { | |
int acc = 1; | |
while (true) { | |
BigInt value = BigInt.parse(acc.toRadixString(2)); | |
BigInt mod = value % d; | |
if (mod == BigInt.zero) { | |
return value; | |
} | |
acc++; | |
if (acc > 10000000) { | |
print('overflow'); | |
return d; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment