Jump to content

Vampire number: Difference between revisions

Added Forth entry.
(Added Easylang)
imported>CyD
(Added Forth entry.)
Line 1,400:
24959017348650 { 2947050 8469153 } { 2949705 8461530 } { 4125870 6049395 } { 4129587 6043950 } { 4230765 5899410 }
</pre>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
 
<syntaxhighlight lang="Forth">: sqrt ( u -- sqrt ) ( Babylonian method )
dup 2/ ( first square root guess is half )
dup 0= if drop exit then ( sqrt[0]=0, sqrt[1]=1 )
begin dup >r 2dup / r> + 2/ ( stack: square old-guess new-guess )
2dup > while ( as long as guess is decreasing )
nip repeat ( forget old-guess and repeat )
drop nip ;
 
: ndigits ( n -- n ) 10 / dup 0<> if recurse then 1+ ;
 
: dtally ( n -- n )
10 /mod
dup 0<> if recurse then
swap 6 * 1 swap lshift + ;
 
: ?product ( x a b -- f ) * = ;
: ?dtally ( x a b -- f ) dtally rot dtally rot dtally rot + = ;
: ?ndigits ( a b -- f ) ndigits swap ndigits = ;
: ?0trail ( a b -- f ) 10 mod 0= swap 10 mod 0= and invert ;
 
: ?fang ( x a -- f )
2dup / 2>r
dup 2r@ ?product
swap 2r@ ?dtally and
2r@ ?ndigits and
2r> ?0trail and ;
 
: next-fang ( n a -- false | a true )
over sqrt swap 1+ ?do
dup i ?fang if drop i true unloop exit then
loop drop false ;
 
: ?vampire ( n -- false | a true ) 0 next-fang ;
: next-vampire ( n -- n a ) begin 1+ dup ?vampire until ;
 
: .product ( n a -- ) dup . ." x " / . ." = " ;
 
: .vampire ( n a -- )
cr
begin 2dup .product
over swap next-fang
while repeat
. ;
 
: .fangs ( n -- ) dup ?vampire if .vampire else cr . ." is not vampiric." then ;
 
: vampires ( n -- )
1 swap
0 do next-vampire over swap .vampire loop drop ;
 
25 vampires
16758243290880 .fangs
24959017348650 .fangs
14593825548650 .fangs</syntaxhighlight>
 
{{out}}<pre>25 vampires
21 x 60 = 1260
15 x 93 = 1395
35 x 41 = 1435
30 x 51 = 1530
21 x 87 = 1827
27 x 81 = 2187
80 x 86 = 6880
201 x 510 = 102510
260 x 401 = 104260
210 x 501 = 105210
204 x 516 = 105264
150 x 705 = 105750
135 x 801 = 108135
158 x 701 = 110758
152 x 761 = 115672
161 x 725 = 116725
167 x 701 = 117067
141 x 840 = 118440
201 x 600 = 120600
231 x 534 = 123354
281 x 443 = 124483
152 x 824 = 125248
231 x 543 = 125433
204 x 615 = 246 x 510 = 125460
251 x 500 = 125500 ok
16758243290880 .fangs
1982736 x 8452080 = 2123856 x 7890480 = 2751840 x 6089832 = 2817360 x 5948208 = 16758243290880 ok
24959017348650 .fangs
2947050 x 8469153 = 2949705 x 8461530 = 4125870 x 6049395 = 4129587 x 6043950 = 4230765 x 5899410 = 24959017348650 ok
14593825548650 .fangs
14593825548650 is not vampiric. ok</pre>
 
 
=={{header|FreeBASIC}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.