Pernicious numbers: Difference between revisions

m
Added Easylang
(Add Miranda)
m (Added Easylang)
 
(8 intermediate revisions by 5 users not shown)
Line 22:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F popcountis_prime(n)
R bin(n).count(‘1’)
 
F is_prime(n)
I n < 2
R 0B
Line 36 ⟶ 33:
V cnt = 0
L
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)
cnt++
Line 45 ⟶ 42:
print()
L(i) 888888877..888888888
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)</syntaxhighlight>
 
Line 188 ⟶ 185:
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|Action!}}==
Action! integers are limited to 16 bits, so this implements 32 bit addition and multiplication by 8-bit values to handle the larger numbers.
<syntaxhighlight lang="action!">
;;; find some pernicious numbers - numbers where the population count is prime
 
;;; As the task requires 32 bit integers, this implements 32-bit unsigend
;;; integer addition and multiplication by an 8-bit integer.
;;; The 32-bit values are stored in 4 separate bytes
 
 
;;; returns the population (number of bits on) of the non-negative integer n
BYTE FUNC population( CARD n )
CARD number
BYTE result
number = n
result = 0;
WHILE number > 0 DO
IF number AND 1 THEN result ==+ 1 FI
number ==/ 2
OD
RETURN( result )
 
;;; returns TRUE if n is a prime; n must be <= 32
BYTE FUNC isSmallPrime( BYTE n )
BYTE result
IF n = 2 THEN result = 1
ELSEIF ( n AND 1 ) = 0 THEN result = 0
ELSEIF n = 1 OR n = 9 OR n = 15
OR n = 21 OR n = 25 OR n = 27
THEN result = 0
ELSE result = 1
FI
RETURN( result )
 
;;; returns TRUE if n is pernicious, FALSE otherwise
BYTE FUNC isPernicious( CARD n ) RETURN( isSmallPrime( population( n ) ) )
 
;;; returns TRUE if the 32 bit integer in i1, i2, i3, i4 is pernicious,
;;; FALSE otherwise
BYTE FUNC isPernicious32( BYTE i1, i2, i3, i4 )
BYTE p
p = population( i1 ) + population( i2 )
+ population( i3 ) + population( i4 )
RETURN( isSmallPrime( p ) )
 
;;; adds b to the 32 bit unsigned integer in i1, i2, i3 and i4
PROC i32add8( BYTE POINTER i1, i2, i3, i4, BYTE b )
CARD c1, c2, c3, c4
 
c1 = i1^ c2 = i2^ c3 = i3^ c4 = i4^
c4 ==+ b
i4 ^= c4 MOD 256
c3 ==+ c4 / 256
i3 ^= c3 MOD 256
c2 ==+ c3 / 256
i2 ^= c2 MOD 256
c1 ==+ c2 / 256
i1 ^= c1 MOD 256
 
RETURN
;;; multiplies the 32 bit unsigned integer in i1, i2, i3 and i4 by b
PROC i32mul8( BYTE POINTER i1, i2, i3, i4, BYTE b )
CARD c1, c2, c3, c4, r
 
c1 = i1^ c2 = i2^ c3 = i3^ c4 = i4^
 
r = c4 * b
i4 ^= r MOD 256
r = ( c3 * b ) + ( r / 256 )
i3 ^= r MOD 256
r = ( c2 * b ) + ( r / 256 )
i2 ^= r MOD 256
r = ( c1 * b ) + ( r / 256 )
i1 ^= r MOD 256
 
RETURN
;;; find the first 25 pernicious numbers
PROC Main()
BYTE perniciousCount, i
BYTE i81, i82, i83, i84
BYTE p81, p82, p83, p84
 
perniciousCount = 0
i = 0
WHILE perniciousCount < 25 DO
IF isPernicious( i ) THEN
; found a pernicious number
PrintB( i )Put(' )
perniciousCount ==+ 1
FI
i ==+ 1
OD
PutE()
 
; find the pernicious numbers between 888 888 877 and 888 888 888
 
; form 888 888 800 in i81, i82, i83 and i84
i81 = 0 i82 = 0 i83 = 0 i84 = 88 ; 88
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 8 800
i32add8( @i81, @i82, @i83, @i84, 88 ) ; 8 888
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 888 800
i32add8( @i81, @i82, @i83, @i84, 88 ) ; 888 888
i32mul8( @i81, @i82, @i83, @i84, 10 ) ; 8 888 880
i32add8( @i81, @i82, @i83, @i84, 8 ) ; 8 888 888
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 888 888 800
 
FOR i = 77 TO 88 DO
p81 = i81 p82 = i82 p83 = i83 p84 = i84
i32add8( @p81, @p82, @p83, @p84, i )
IF isPernicious32( p81, p82, p83, p84 )
THEN
print( "8888888" )PrintB( i )Put(' )
FI
OD
PutE()
RETURN
</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
Line 1,163 ⟶ 1,285:
1_421_120_880 Pernicious numbers in the unsigned 32 bit range in less than 48 seconds with this line:
<syntaxhighlight lang="d">uint.max.iota.filter!pernicious.walkLength.writeln;</syntaxhighlight>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func popc n .
while n > 0
r += n mod 2
n = n div 2
.
return r
.
n = 1
while cnt < 25
if isprim popc n = 1
write n & " "
cnt += 1
.
n += 1
.
print ""
n = 1
for n = 888888877 to 888888888
if isprim popc n = 1
write n & " "
.
.
</syntaxhighlight>
 
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|EchoLisp}}==
Line 1,583 ⟶ 1,751:
 
=={{header|Fōrmulæ}}==
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Pernicious_numbers}}
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Solution'''
 
[[File:Fōrmulæ - Pernicious numbers 01.png]]
 
'''Case 1. Display the first 25 pernicious numbers (in decimal)'''
 
[[File:Fōrmulæ - Pernicious numbers 02.png]]
 
[[File:Fōrmulæ - Pernicious numbers 03.png]]
 
'''Case 2. display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive).'''
 
[[File:Fōrmulæ - Pernicious numbers 04.png]]
 
[[File:Fōrmulæ - Pernicious numbers 05.png]]
In '''[https://formulae.org/?example=Pernicious_numbers this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
Line 3,635 ⟶ 3,816:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var pernicious = Fn.new { |w|
var ff = 2.pow(32) - 1
var mask1 = (ff / 3).floor
1,985

edits