Frobenius numbers: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24:
{{trans|Python}}
<
I v <= 1
R 0B
Line 53:
L.break
print(n‘ => ’f)
pn = i</
{{out}}
Line 86:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
INT FUNC NextPrime(INT p BYTE ARRAY primes)
Line 114:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Frobenius_numbers.png Screenshot from Atari 8-bit computer]
Line 122:
=={{header|ALGOL 68}}==
<
# Frobenius(n) = ( prime(n) * prime(n+1) ) - prime(n) - prime(n+1) #
# reurns a list of primes up to n #
Line 150:
print( ( " ", whole( frobenius number, 0 ) ) )
OD
END</
{{out}}
<pre>
Line 158:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<pre>1 7 23 59 119 191 287 395 615 839 1079 1439 1679 1931 2391 3015 3479 3959 4619 5039 5615 6395 7215 8447 9599</pre>
=={{header|AppleScript}}==
<
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
Line 193:
end Frobenii
Frobenii(9999)</
{{output}}
<
=={{header|Arturo}}==
<
frobenius: function [n] -> sub sub primes\[n] * primes\[n+1] primes\[n] primes\[n+1]
Line 211:
loop split.every:10 chop lst 'a ->
print map a => [pad to :string & 5]</
{{out}}
Line 220:
=={{header|AutoHotkey}}==
<
loop {
if isprime(i+=2) {
Line 245:
return false
return true
}</
{{out}}
<pre> 1 7 23 59 119
Line 254:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FROBENIUS_NUMBERS.AWK
# converted from FreeBASIC
Line 283:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 293:
=={{header|BASIC}}==
<
20 LM = 10000
30 M = SQR(LM)+1
Line 305:
110 FOR N=0 TO C-2
120 PRINT P(N)*P(N+1)-P(N)-P(N+1),
130 NEXT N</
{{out}}
<pre> 1 7 23 59 119
Line 315:
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
n = 0
lim = 10000
Line 334:
next i
end
</syntaxhighlight>
=={{header|BCPL}}==
<
manifest $( limit = 10000 $)
Line 391:
writef("%N*N", frob(primes, n))
freevec(primes)
$)</
{{out}}
<pre>1
Line 420:
=={{header|C}}==
<
#include <stdlib.h>
#include <math.h>
Line 457:
return 0;
}</
{{out}}
<pre>1
Line 487:
=={{header|C sharp|C#}}==
Asterisks mark the non-primes among the numbers.
<
class Program {
Line 509:
if (!flags[j]) { yield return j;
for (int k = sq, i = j << 1; k <= lim; k += i) flags[k] = true; }
for (; j <= lim; j += 2) if (!flags[j]) yield return j; } }</
{{out}}
Line 534:
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <iomanip>
#include <iostream>
Line 573:
}
std::cout << '\n';
}</
{{out}}
Line 598:
=={{header|Cowgol}}==
<
const LIMIT := 10000;
Line 652:
print_nl();
n := n + 1;
end loop;</
{{out}}
<pre>1
Line 682:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
"Frobenius numbers < 10,000:" print
Line 688:
[ nip dup next-prime ] [ * ] [ [ - ] dip - ] 2tri
dup 10,000 <
] [ . ] while 3drop</
{{out}}
<pre style="height:14em">
Line 720:
=={{header|Fermat}}==
<
for n = 1 to 25 do !!Frobenius(n) od</
{{out}}
<pre>
Line 752:
=={{header|FreeBASIC}}==
<
dim as integer pn=2, n=0, f
Line 763:
pn = i
end if
next i</
{{out}}
<pre>
Line 796:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 821:
}
fmt.Printf("\n\n%d such numbers found.\n", len(frobenius))
}</
{{out}}
Line 834:
=={{header|Haskell}}==
<
where sieve (x:xs) = x : sieve (filter (\y -> y `mod` x /= 0) xs)
frobenius = zipWith (\a b -> a*b - a - b) primes (tail primes)</
<pre>λ> takeWhile (< 10000) frobenius
Line 843:
=={{header|J}}==
<
echo frob i. 25</
(Note that <code>frob</code> counts prime numbers starting from 0 (which gives 2), so for some contexts the function to calculate frobenius numbers would be <code>frob@<:</code>.)
Line 853:
=={{header|Java}}==
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<
public static void main(String[] args) {
final int limit = 1000000;
Line 888:
return true;
}
}</
{{out}}
Line 921:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<
# specify `null` or `infinite` to generate an unbounded stream.
def frobenius:
Line 934:
.frob);
9999 | frobenius</
{{out}}
<pre>
Line 965:
=={{header|Julia}}==
<
const primeslt10k = primes(10000)
Line 989:
testfrobenius()
</
<pre>
Frobenius numbers less than 1,000,000 (an asterisk marks the prime ones).
Line 1,012:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
fn[n_] := Prime[n] Prime[n + 1] - Prime[n] - Prime[n + 1]
a = -1;
Line 1,022:
If[a < 10^4, AppendTo[res, a]]
]
res</
{{out}}
<pre>{1,7,23,59,119,191,287,395,615,839,1079,1439,1679,1931,2391,3015,3479,3959,4619,5039,5615,6395,7215,8447,9599}</pre>
Line 1,028:
=={{header|Nim}}==
As I like iterators, I used one for (odd) primes and one for Frobenius numbers. Of course, there are other ways to proceed.
<
func isOddPrime(n: Positive): bool =
Line 1,060:
var result = toSeq(frobenius(10_000))
echo "Found $1 Frobenius numbers less than $2:".format(result.len, N)
echo result.join(" ")</
{{out}}
Line 1,068:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,081:
# process a list with a 2-wide sliding window
my $limit = 10_000;
say "\n" . join ' ', grep { $_ < $limit } slide { $a * $b - $a - $b } @{primes($limit)};</
{{out}}
<pre>25 matching numbers:
Line 1,089:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">to</span> <span style="color: #000000;">6</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
Line 1,105:
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">frob</span><span style="color: #0000FF;">),</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">frob</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,114:
=={{header|Python}}==
<
#!/usr/bin/python
Line 1,147:
print (n, ' => ', f)
pn = i
</syntaxhighlight>
=={{header|PL/M}}==
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,223:
END;
CALL EXIT;
EOF</
{{out}}
<pre>1
Line 1,253:
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
Procedure isPrime(v.i)
If v < = 1 : ProcedureReturn #False
Line 1,290:
CloseConsole()
End
</syntaxhighlight>
{{out}}
<pre>
Line 1,322:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
given (^1000).grep( *.is-prime ).rotor(2 => -1)
.map( { (.[0] * .[1] - .[0] - .[1]) } ).grep(* < 10000);</
{{out}}
<pre>25 matching numbers
Line 1,332:
=={{header|REXX}}==
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 10000 /* " " " " " " */
Line 1,370:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # Ps; assign next P; P squared*/
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,384:
=={{header|Ring}}==
<
? "working..." + nl + "Frobenius numbers are:"
Line 1,407:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</
{{out}}
<pre>working...
Line 1,422:
=={{header|Rust}}==
<
// primal = "0.3"
Line 1,452:
}
println!();
}</
{{out}}
Line 1,477:
=={{header|Sidef}}==
<
prime(n) * prime(n+1) - prime(n) - prime(n+1)
}
Line 1,487:
take(n)
}
}</
{{out}}
<pre>
Line 1,497:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,510:
System.print("Frobenius numbers under 10,000:")
for (chunk in Lst.chunks(frobenius, 9)) Fmt.print("$,5d", chunk)
System.print("\n%(frobenius.count) such numbers found.")</
{{out}}
Line 1,523:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,548:
Text(0, " Frobenius numbers found below 10,000.
");
]</
{{out}}
Line 1,560:
=={{header|Yabasic}}==
{{trans|PureBasic}}
<
sub isPrime(v)
if v < 2 then return False : fi
Line 1,584:
next i
end
</syntaxhighlight>
{{out}}
<pre>
|