Magnanimous numbers: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33:
=={{header|ALGOL 68}}==
<
# digits ab=nd evaluatinf the sum results in a prime in all cases #
# returns the first n magnanimous numbers #
Line 78:
print magnanimous( m, 241, 250, "Magnanimous numbers 241-250" );
print magnanimous( m, 391, 400, "Magnanimous numbers 391-400" )
END</
{{out}}
<pre>
Line 90:
=={{header|ALGOL W}}==
<
% find some Magnanimous numbers - numbers where inserting a "+" between %
% any two of the digits and evaluating the sum results in a prime number %
Line 228:
write( "Last magnanimous number found: ", mPos, " = ", lastM )
end
end.</
{{out}}
<pre>
Line 243:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MAGNANIMOUS_NUMBERS.AWK
# converted from C
Line 286:
printf("\n")
}
</syntaxhighlight>
{{out}}
<pre>
Line 296:
=={{header|C}}==
{{trans|Go}}
<
#include <string.h>
Line 383:
list_mags(391, 400, 1, 10);
return 0;
}</
{{out}}
Line 400:
=={{header|C#|CSharp}}==
<
class Program {
Line 424:
if (c == 240) WriteLine ("\n\n241st through 250th{0}", mn);
if (c == 390) WriteLine ("\n\n391st through 400th{0}", mn); } }
}</
{{out}}
Line 439:
=={{header|C++}}==
<
#include <iostream>
Line 500:
std::cout << '\n';
return 0;
}</
{{out}}
Line 519:
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// Generate Magnanimous numbers. Nigel Galloway: March 20th., 2020
let rec fN n g = match (g/n,g%n) with
Line 526:
|_ -> false
let Magnanimous = let Magnanimous = fN 10 in seq{yield! {0..9}; yield! Seq.initInfinite id |> Seq.skip 10 |> Seq.filter Magnanimous}
</syntaxhighlight>
===The Tasks===
;First 45
<
Magnanimous |> Seq.take 45 |> Seq.iter (printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 539:
;Magnanimous[241] to Magnanimous[250]
<
Magnanimous |> Seq.skip 240 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;
</syntaxhighlight>
{{out}}
<pre>
Line 548:
;Magnanimous[391] to Magnanimous[400]
<
Magnanimous |> Seq.skip 390 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;
</syntaxhighlight>
{{out}}
<pre>
Line 558:
{{trans|Julia}}
{{works with|Factor|0.99 2020-01-23}}
<
math.primes math.ranges prettyprint sequences ;
Line 576:
[ "241st through 250th magnanimous numbers" print 240 250 show ]
[ "391st through 400th magnanimous numbers" print 390 400 show ]
tri</
{{out}}
<pre>
Line 592:
=={{header|FreeBASIC}}==
<
#include "isprime.bas"
Line 624:
print i+1,magn(i)
next i
</syntaxhighlight>
{{out}}
<pre>1 0
Line 693:
=={{header|Go}}==
<
import "fmt"
Line 782:
listMags(241, 250, 1, 10)
listMags(391, 400, 1, 10)
}</
{{out}}
Line 799:
=={{header|Haskell}}==
<
import Data.List ( (!!) )
Line 845:
putStrLn "391'st to 400th magnanimous numbers:"
putStr $ show ( numbers !! 390 )
putStrLn ( foldl1 ( ++ ) $ map(\n -> printInWidth n 8 ) $ drop 391 numbers)</
{{out}}
<pre>First 45 magnanimous numbers:
Line 860:
=={{header|Java}}==
<
import java.util.ArrayList;
import java.util.List;
Line 1,046:
}
</syntaxhighlight>
{{out}}
Line 1,085:
'''Preliminaries'''
<
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
def divrem($x; $y):
[$x/$y|floor, $x % $y];
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq">
def ismagnanimous:
. as $n
Line 1,111:
| "First 45 magnanimous numbers:", .[:45],
"\n241st through 250th magnanimous numbers:", .[241:251],
"\n391st through 400th magnanimous numbers:", .[391:]</
{{out}}
<pre>
Line 1,126:
=={{header|Julia}}==
<
function ismagnanimous(n)
Line 1,152:
println("\n241st through 250th magnanimous numbers:\n", mag400[241:250])
println("\n391st through 400th magnanimous numbers:\n", mag400[391:400])
</
<pre>
First 45 magnanimous numbers:
Line 1,166:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
MagnanimousNumberQ[Alternatives @@ Range[0, 9]] = True;
MagnanimousNumberQ[n_Integer] := AllTrue[Range[IntegerLength[n] - 1], PrimeQ[Total[FromDigits /@ TakeDrop[IntegerDigits[n], #]]] &]
Line 1,172:
sel[[;; 45]]
sel[[241 ;; 250]]
sel[[391 ;; 400]]</
{{out}}
<pre>{0,1,2,3,4,5,6,7,8,9,11,12,14,16,20,21,23,25,29,30,32,34,38,41,43,47,49,50,52,56,58,61,65,67,70,74,76,83,85,89,92,94,98,101,110}
Line 1,179:
=={{header|Nim}}==
<
if n < 2: return
if n mod 2 == 0: return n == 2
Line 1,226:
elif i > 400:
break</
{{out}}
Line 1,242:
Eliminating all numbers, which would sum to 5 in the last digit.<br>
On TIO.RUN found all til 569 84448000009 0.715 s
<
//Magnanimous Numbers
//algorithm find only numbers where all digits are even except the last
Line 1,624:
readln;
{$ENDIF}
end.</
{{out}}
<pre style="height:180px">
Line 2,209:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 2,235:
say "\n391st through 400th magnanimous numbers\n".
join ' ', @M[390..399];</
{{out}}
<pre>First 45 magnanimous numbers
Line 2,249:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">magnanimous</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 2,271:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"magnanimous numbers[241..250]: %v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">mag</span><span style="color: #0000FF;">[</span><span style="color: #000000;">241</span><span style="color: #0000FF;">..</span><span style="color: #000000;">250</span><span style="color: #0000FF;">]})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"magnanimous numbers[391..400]: %v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">mag</span><span style="color: #0000FF;">[</span><span style="color: #000000;">391</span><span style="color: #0000FF;">..</span><span style="color: #000000;">400</span><span style="color: #0000FF;">]})</span>
<!--</
{{out}}
<pre>
Line 2,281:
=={{header|PicoLisp}}==
<
(let M 1
(loop
Line 2,340:
(println (head 45 Lst))
(println (head 10 (nth Lst 241)))
(println (head 10 (nth Lst 391))) )</
{{out}}
<pre>
Line 2,351:
This sample can be compiled with the original 8080 PL/M compiler and run under CP/M (or a clone/emulator).
<br>THe original 8080 PL/M only supports 8 and 16 bit quantities, so this only shows magnanimous numbers up to the 250th.
<
/* ANY TWO OF THE DIGITS AND EVALUATING THE SUM RESULTS IN A PRIME */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
Line 2,509:
END;
CALL PRINT$NL;
EOF</
{{out}}
<pre>
Line 2,523:
{{works with|Rakudo|2020.02}}
<syntaxhighlight lang="raku"
my int $last;
(1 ..^ .chars).map: -> \c { $last = 1 and last unless (.substr(0,c) + .substr(c)).is-prime }
Line 2,546:
put "\n391st through 400th magnanimous numbers";
put @magnanimous[390..399];</
{{out}}
<pre>First 45 magnanimous numbers
Line 2,563:
<br>The '''magna''' function (magnanimous) was quite simple to code and pretty fast, it includes the 1<sup>st</sup> and last digit parity test.
<br>By far, the most CPU time was in the generation of primes.
<
parse arg bet.1 bet.2 bet.3 highP . /*obtain optional arguments from the CL*/
if bet.1=='' | bet.1=="," then bet.1= 1..45 /* " " " " " " */
Line 2,607:
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump #Ps; P──►@.assign P; P^2; P flag*/
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,625:
=={{header|Ring}}==
<
load "stdlib.ring"
n = -1
Line 2,698:
txt = txt + "]"
see txt
</syntaxhighlight>
<pre>
Magnanimous numbers 1-45:
Line 2,708:
=={{header|Ruby}}==
{{trans|Sidef}}
<
magnanimouses = Enumerator.new do |y|
Line 2,722:
puts "\n391st through 400th magnanimous numbers:"
puts magnanimouses.first(400).last(10).join(' ')
</syntaxhighlight>
{{out}}
<pre>First 45 magnanimous numbers:
Line 2,735:
=={{header|Rust}}==
<
if n < 2 {
return false;
Line 2,788:
}
println!();
}</
{{out}}
Line 2,805:
=={{header|Sidef}}==
<
1..n.ilog10 -> all {|k|
sum(divmod(n, k.ipow10)).is_prime
Line 2,818:
say "\n391st through 400th magnanimous numbers:"
say is_magnanimous.first(400).last(10).join(' ')</
{{out}}
<pre>
Line 2,833:
=={{header|Swift}}==
{{trans|Rust}}
<
func isPrime(_ n: Int) -> Bool {
Line 2,886:
print(n, terminator: " ")
}
print()</
{{out}}
Line 2,904:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,937:
End If : l += 1 : End While
End Sub
End Module</
{{out}}
<pre>First 45 magnanimous numbers:
Line 2,954:
{{libheader|Wren-math}}
{{trans|Go}}
<
import "/math" for Int
Line 2,992:
listMags.call(1, 45, 3, 15)
listMags.call(241, 250, 1, 10)
listMags.call(391, 400, 1, 10)</
{{out}}
|