Deceptive numbers: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
(New post.)
Line 307: Line 307:
<syntaxhighlight lang="j"> deceptives 21
<syntaxhighlight lang="j"> deceptives 21
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001</syntaxhighlight>
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001</syntaxhighlight>

=={{header|Java}}==
<syntaxhighlight lang="java">

public final class DeceptiveNumbers {

public static void main(String[] aArgs) {
int n = 7;
int count = 0;
while ( count < 100 ) {
if ( isDeceptive(n) ) {
System.out.print(String.format("%6d%s", n, ( ++count % 10 == 0 ? "\n" : " " )));
}
n += 1;
}
}
private static boolean isDeceptive(int aN) {
if ( aN % 2 != 0 && aN % 3 != 0 && aN % 5 != 0 && modulusPower(10, aN - 1, aN) == 1 ) {
for ( int divisor = 7; divisor < Math.sqrt(aN); divisor += 6 ) {
if ( aN % divisor == 0 || aN % ( divisor + 4 ) == 0 ) {
return true;
}
}
}
return false;
}
private static long modulusPower(long aBase, long aExponent, long aModulus) {
if ( aModulus == 1 ) {
return 0;
}
aBase %= aModulus;
long result = 1;
while ( aExponent > 0 ) {
if ( ( aExponent & 1 ) == 1 ) {
result = ( result * aBase ) % aModulus;
}
aBase = ( aBase * aBase ) % aModulus;
aExponent >>= 1;
}
return result;
}
}
</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
65311 66991 67861 68101 75361 79003 82513 83119 94139 95161
97273 97681 100001 101101 101491 102173 108691 113201 115627 115921
118301 118957 122221 126217 128713 130351 131821 134821 134863 137137
137149 138481 139231 145181 147001 148417 152551 158497 162401 164761
166499 170017 172081 179881 188191 188269 188461 188501 196651 201917
</pre>


=={{header|jq}}==
=={{header|jq}}==