Deceptive numbers: Difference between revisions

use the math; avoid an unnecessary multiplication
m (→‎{{header|jq}}: indentation)
(use the math; avoid an unnecessary multiplication)
Line 102:
unsigned modpow(unsigned b, unsigned e, unsigned m)
{
unsigned p = 1;
whilefor (p = 1; e; e >>= 1) {
if (e & 1)
p = p * b % m;
b = b * b % m;
e >>= 1;
}
return p;
Line 118 ⟶ 117:
for (x = 7; x * x <= n; x += 6) {
if (!(n % x && n % (x + 4)))
return modpow(10, n - 1, n * 9) == 1;
}
}
Line 127 ⟶ 126:
{
unsigned c, i = 49;
for (c = 0; c != 1850; ++i) {
if (is_deceptive(i)) {
printf(" %u", i);
Line 136 ⟶ 135:
}</syntaxhighlight>
{{out}}
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973</pre>
 
=={{header|C++}}==
Line 483 ⟶ 482:
in
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 && loop 7 &&
modpow (n * 9) 10 (pred n) = 1
 
let () =
Line 750 ⟶ 749:
 
def is_deceptive(n):
if n & 1 and n % 3 and n % 5 and pow(10, n - 1, n * 9) == 1:
for d in range(7, isqrt(n) + 1, 6):
if not (n % d and n % (d + 4)): return True
559

edits