Jump to content

Magnanimous numbers: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 33:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find some magnanimous numbers - numbers where inserting a + between any #
# 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</langsyntaxhighlight>
{{out}}
<pre>
Line 90:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% 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.</langsyntaxhighlight>
{{out}}
<pre>
Line 243:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MAGNANIMOUS_NUMBERS.AWK
# converted from C
Line 286:
printf("\n")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 296:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 383:
list_mags(391, 400, 1, 10);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 400:
 
=={{header|C#|CSharp}}==
<langsyntaxhighlight lang="csharp">using System; using static System.Console;
 
class Program {
Line 424:
if (c == 240) WriteLine ("\n\n241st through 250th{0}", mn);
if (c == 390) WriteLine ("\n\n391st through 400th{0}", mn); } }
}</langsyntaxhighlight>
 
{{out}}
Line 439:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 500:
std::cout << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 519:
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
===The Tasks===
;First 45
 
<langsyntaxhighlight lang="fsharp">
Magnanimous |> Seq.take 45 |> Seq.iter (printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 539:
;Magnanimous[241] to Magnanimous[250]
 
<langsyntaxhighlight lang="fsharp">
Magnanimous |> Seq.skip 240 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 548:
;Magnanimous[391] to Magnanimous[400]
 
<langsyntaxhighlight lang="fsharp">
Magnanimous |> Seq.skip 390 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 558:
{{trans|Julia}}
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: grouping io kernel lists lists.lazy math math.functions
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</langsyntaxhighlight>
{{out}}
<pre>
Line 592:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 624:
print i+1,magn(i)
next i
</syntaxhighlight>
</lang>
{{out}}
<pre>1 0
Line 693:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 782:
listMags(241, 250, 1, 10)
listMags(391, 400, 1, 10)
}</langsyntaxhighlight>
 
{{out}}
Line 799:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split ( chunksOf )
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)</langsyntaxhighlight>
{{out}}
<pre>First 45 magnanimous numbers:
Line 860:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,046:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,085:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
def divrem($x; $y):
[$x/$y|floor, $x % $y];
</syntaxhighlight>
</lang>
'''The Task'''
<syntaxhighlight lang="jq">
<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:]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,126:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
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])
</langsyntaxhighlight>{{out}}
<pre>
First 45 magnanimous numbers:
Line 1,166:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Clear[MagnanimousNumberQ]
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]]</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Nimlang="nim">func isPrime(n: Natural): bool =
if n < 2: return
if n mod 2 == 0: return n == 2
Line 1,226:
 
elif i > 400:
break</langsyntaxhighlight>
 
{{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
<langsyntaxhighlight lang="pascal">program Magnanimous;
//Magnanimous Numbers
//algorithm find only numbers where all digits are even except the last
Line 1,624:
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre style="height:180px">
Line 2,209:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,235:
 
say "\n391st through 400th magnanimous numbers\n".
join ' ', @M[390..399];</langsyntaxhighlight>
{{out}}
<pre>First 45 magnanimous numbers
Line 2,249:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,281:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de **Mod (X Y N)
(let M 1
(loop
Line 2,340:
(println (head 45 Lst))
(println (head 10 (nth Lst 241)))
(println (head 10 (nth Lst 391))) )</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="plm">100H: /* FIND SOME MAGNANIMOUS NUMBERS - THOSE WHERE INSERTING '+' BETWEEN */
/* 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</langsyntaxhighlight>
{{out}}
<pre>
Line 2,523:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>my @magnanimous = lazy flat ^10, (10 .. 1001).map( {
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];</langsyntaxhighlight>
{{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.
<langsyntaxhighlight REXXlang="rexx">/*REXX pgm finds/displays magnanimous #s (#s with a inserted + sign to sum to a prime).*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,625:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
n = -1
Line 2,698:
txt = txt + "]"
see txt
</syntaxhighlight>
</lang>
<pre>
Magnanimous numbers 1-45:
Line 2,708:
=={{header|Ruby}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="ruby">require "prime"
 
magnanimouses = Enumerator.new do |y|
Line 2,722:
puts "\n391st through 400th magnanimous numbers:"
puts magnanimouses.first(400).last(10).join(' ')
</syntaxhighlight>
</lang>
{{out}}
<pre>First 45 magnanimous numbers:
Line 2,735:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_prime(n: u32) -> bool {
if n < 2 {
return false;
Line 2,788:
}
println!();
}</langsyntaxhighlight>
 
{{out}}
Line 2,805:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_magnanimous(n) {
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(' ')</langsyntaxhighlight>
{{out}}
<pre>
Line 2,833:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">import Foundation
 
func isPrime(_ n: Int) -> Bool {
Line 2,886:
print(n, terminator: " ")
}
print()</langsyntaxhighlight>
 
{{out}}
Line 2,904:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System, System.Console
 
Module Module1
Line 2,937:
End If : l += 1 : End While
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>First 45 magnanimous numbers:
Line 2,954:
{{libheader|Wren-math}}
{{trans|Go}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
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)</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.