Rare numbers: Difference between revisions

5,527 bytes added ,  15 days ago
(Marked Quackery incorrect)
 
(12 intermediate revisions by 8 users not shown)
Line 26:
:*   author's  website:   [http://www.shyamsundergupta.com/rare.html rare numbers]   by Shyam Sunder Gupta.     (lots of hints and some observations).
<br><br>
 
=={{header|ALGOL 68}}==
{{Trans|FreeBASIC}}
which is
{{Trans|Phix}}
(naive version)
<syntaxhighlight lang="algol68">
PROC revn = ( LONG INT na, nda )LONG INT:
BEGIN
LONG INT n := na, nd := nda, r := 0, i := 0;
WHILE i +:= 1;
i <= nd
DO
r *:= 10 +:= ( n MOD 10 );
n OVERAB 10
OD;
r
END # revn # ;
 
LONG INT nd := 2, count := 0, lim := 90, n := 20;
 
DO
n +:= 1;
LONG INT r = revn( n, nd );
IF r < n THEN
LONG INT s = n + r, d = n - r;
IF IF ODD nd
THEN d MOD 1089 = 0
ELSE s MOD 121 = 0
FI
THEN
IF LONG REAL root s = long sqrt( s );
root s = ENTIER root s
THEN
IF LONG REAL root d = long sqrt( d );
root d = ENTIER root d
THEN
count +:= 1;
print( ( whole( count, 0 ), ": ", whole( n, 0 ), newline ) );
IF count >= 5 THEN stop FI
FI
FI
FI;
IF n = lim
THEN
lim *:= 10;
nd +:= 1;
n := ( lim OVER 9 ) * 2
FI
FI
OD
</syntaxhighlight>
{{out}}
<pre>
1: 65
2: 621770
3: 281089082
4: 2022652202
5: 2042832002
</pre>
 
=={{header|C sharp|C#}}==
Line 1,153 ⟶ 1,213:
39: 8,650,327,689,541,457
40: 8,650,349,867,341,457</pre>
 
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight lang=easylang>
fastfunc next n .
while 1 = 1
n += 1
h = n
nrev = 0
while h > 0
nrev = nrev * 10 + h mod 10
h = h div 10
.
if sqrt (n + nrev) mod 1 = 0
if n - nrev >= 1 and sqrt (n - nrev) mod 1 = 0
return n
.
.
.
.
for cnt to 5
n = next n
print n
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
Line 1,243 ⟶ 1,329:
Elapsed Time: 11275839 ms for length 17
</pre>
 
 
=={{header|FreeBASIC}}==
Line 3,753 ⟶ 3,838:
39: 8,650,327,689,541,457
40: 8,650,349,867,341,457</pre>
 
=={{header|Lambdatalk}}==
Just coding the definition of a rare number without any optimization.
<syntaxhighlight lang="Scheme">
 
{def lt_israre
{lambda {:n}
{let { {:n :n}
{:r {W.reverse :n}}
} {if {and {> :n :r}
{isInt {sqrt {+ :n :r}}}
{isInt {sqrt {- :n :r}}}}
then :n
else}}}}
-> lt_israre
 
{S.map lt_israre {S.serie 1 700000}}
-> 65 621770 // computed in 7650ms
 
Testing:
 
{S.map lt_israre {S.serie 1 280000000}}
-> ... crushes Firefox working in my small iPad Pro.
 
And so I ask javascript some help:
 
LAMBDATALK.DICT["js_israres"] = function() {
var args = arguments[0].trim().split(" "),
i0 = Number( args[0] ),
i1 = Number( args[1] ),
a = [];
 
var israre = function(n) {
var r = Number( n.toString().split("").reverse().join("") );
return (n > r) && (Number.isInteger(Math.sqrt(n+r)))
&& (Number.isInteger(Math.sqrt(n-r)))
};
 
for (var i=i0; i < i1; i++)
if (israre(i)) a.push(i);
return a
};
 
Testing:
 
{js_israres 1 2050000000}
-> [65,621770,281089082,2022652202,2042832002]]
// computed in 784307ms ~ 13 minutes
 
Too slow to try to go further.
 
</syntaxhighlight>
 
=={{header|langur}}==
Line 3,759 ⟶ 3,896:
It could look something like the following (ignoring whatever optimizations the other examples are using), if it was fast enough. I did not have the time/processor to test finding the first 5. The .israre() function appears to return the right answer, tested with individual numbers.
 
<syntaxhighlight lang="langur">val .perfectsquare = fn(.n) { (.n ^/ 2) div 1 }
{{works with|langur|0.8.11}}
<syntaxhighlight lang="langur">val .perfectsquare = f isInteger .n ^/ 2
 
val .israre = ffn(.n) {
val .r = reverse(.n)
if .n == .r: return false
Line 3,770 ⟶ 3,906:
}
 
val .findfirst = ffn(.max) {
for[=[]] .i = 0; len(_for) < .max; .i += 1 {
if .israre(.i) {
_for ~= [.i]
if len(_for) == .max: break
}
}
Line 3,782 ⟶ 3,917:
writeln "the first 5 rare numbers: ", .findfirst(5)</syntaxhighlight>
 
=={{header|Lua}}==
With 0.8.11, the built-in reverse() function will flip the digits of a number. Without this, you could write your own function to do so as follows (if not passed any negative numbers).
{{Trans|Phix|niave version, via FreeBASIC and Algol 68}}
<syntaxhighlight lang="lua">
do -- find the first five rare numbers
 
local function revn ( na )
<syntaxhighlight lang="langur">val .reverse = f toNumber join reverse split .n</syntaxhighlight>
local n, r = na, 0
while n > 0 do
r = r * 10 r = r + ( n % 10 )
n = math.floor( n / 10 )
end
return r
end -- revn
 
local nd, count, lim, n = 2, 0, 90, 20
local oddNd = nd % 2 == 1
while true do
n = n + 1
local r = revn( n )
if r < n then
local s, d = n + r, n - r
local tryThis = false
if oddNd
then tryThis = d % 1089 == 0
else tryThis = s % 121 == 0
end
if tryThis then
local rootS = math.sqrt( s )
if rootS == math.floor(rootS )
then
local rootD = math.sqrt( d )
if rootD == math.floor( rootD )
then
count = count + 1
io.write( count, ": ", n, "\n" )
if count >= 5 then os.exit() end
end
end
end
if n == lim
then
lim = lim * 10
nd = nd + 1
oddNd = not oddNd
n = math.floor( lim / 9 ) * 2
end
end
end
end
</syntaxhighlight>
{{out}}
<pre>
1: 65
2: 621770
3: 281089082
4: 2022652202
5: 2042832002
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 4,563 ⟶ 4,753:
</syntaxhighlight>
===hodgepodge optimized===
A mess of code that can return the first 5 rare numbers in 5m5s1m19s on a i7-7700k using pypy3.9.
 
<syntaxhighlight lang="python">
Line 4,634 ⟶ 4,824:
 
=={{header|Quackery}}==
 
{{incorrect|Quackery|The task' rather pathetic requirement is to find the first 5 rare numbers, can only manage 3 in a week, give me a break}}
Naive version.
 
Line 4,684 ⟶ 4,874:
621770
281089082</pre>
 
=={{header|Racket}}==
===naive===
<syntaxhighlight lang="racket" line>; 20231024 Racket programming naive solution
#lang racket
(require control)
(require text-block/text)
 
(define (is-palindrome n)
(define (digit-list n)
(if (zero? n)
'()
(cons (remainder n 10) (digit-list (quotient n 10)))))
(define (reverse-list lst)
(if (null? lst)
'()
(append (reverse-list (cdr lst)) (list (car lst)))))
(define digits (digit-list n))
(equal? digits (reverse-list digits)))
 
(define (perfect-square? n)
(if (rational? (sqrt n))
(= n (expt (floor (sqrt n)) 2))
false))
 
(define (reverse-number n)
(string->number (string-reverse (number->string n))))
 
(define (find-rare-numbers count)
(define rare-numbers '())
(define i 1)
(define (is-rare? n)
(and (not (is-palindrome n))
(let* ((r (reverse-number n))
(sum (+ n r))
(diff (- n r)))
(and (perfect-square? sum)
(perfect-square? diff)))))
 
(define start-time (current-inexact-milliseconds))
(while (< (length rare-numbers) count)
(cond [(is-rare? i)
(displayln (format "Number: ~a | Elapsed time: ~a ms" i (round (- (current-inexact-milliseconds) start-time))))
(set! rare-numbers (cons i rare-numbers))])
(set! i (+ i 1)))
(reverse rare-numbers))
 
(displayln "The first 5 rare numbers are:")
(for-each (λ (x) (display x) (display "\n")) (find-rare-numbers 5))
</syntaxhighlight>
 
=={{header|Raku}}==
Line 6,261 ⟶ 6,506:
===Traditional===
About 9.5 minutes to find the first 25 rare numbers.
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
import "./fmt" for Fmt
 
class Term {
Line 6,535 ⟶ 6,780:
===Turbo===
Ruffles the feathers a little with a time 5 times quicker than the 'traditional' version.
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Sort
import "./fmt" for Fmt
import "./date" for Date
 
class Z2 {
885

edits