Frobenius numbers: Difference between revisions
Added Easylang
Thundergnat (talk | contribs) m (→{{header|C#|CSharp}}: Regularize header markup to recommended on category page) |
(Added Easylang) |
||
(11 intermediate revisions by 9 users not shown) | |||
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 218:
1079 1439 1679 1931 2391 3015 3479 3959 4619 5039
5615 6395 7215 8447 9599</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">n := 0, i := 1, pn := 2
loop {
if isprime(i+=2) {
if ((f := pn*i - pn - i) > 10000)
break
result .= SubStr(" " f, -3) . (Mod(++n, 5) ? "`t" : "`n")
pn := i
}
}
MsgBox % result
return
isPrime(n, p=1) {
if (n < 2)
return false
if !Mod(n, 2)
return (n = 2)
if !Mod(n, 3)
return (n = 3)
while ((p+=4) <= Sqrt(n))
if !Mod(n, p)
return false
else if !Mod(n, p+=2)
return false
return true
}</syntaxhighlight>
{{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|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FROBENIUS_NUMBERS.AWK
# converted from FreeBASIC
Line 249 ⟶ 283:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 259 ⟶ 293:
=={{header|BASIC}}==
<
20 LM = 10000
30 M = SQR(LM)+1
Line 271 ⟶ 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 281 ⟶ 315:
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
n = 0
lim = 10000
Line 300 ⟶ 334:
next i
end
</syntaxhighlight>
=={{header|BCPL}}==
<
manifest $( limit = 10000 $)
Line 357 ⟶ 391:
writef("%N*N", frob(primes, n))
freevec(primes)
$)</
{{out}}
<pre>1
Line 386 ⟶ 420:
=={{header|C}}==
<
#include <stdlib.h>
#include <math.h>
Line 423 ⟶ 457:
return 0;
}</
{{out}}
<pre>1
Line 453 ⟶ 487:
=={{header|C sharp|C#}}==
Asterisks mark the non-primes among the numbers.
<
class Program {
Line 475 ⟶ 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 500 ⟶ 534:
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <iomanip>
#include <iostream>
Line 539 ⟶ 573:
}
std::cout << '\n';
}</
{{out}}
Line 564 ⟶ 598:
=={{header|Cowgol}}==
<
const LIMIT := 10000;
Line 618 ⟶ 652:
print_nl();
n := n + 1;
end loop;</
{{out}}
<pre>1
Line 645 ⟶ 679:
8447
9599</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
function GetNextPrime(Start: integer): integer;
{Get the next prime number after Start}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
procedure ShowFrobeniusNumbers(Memo: TMemo);
var N,N1,FN,Cnt: integer;
begin
N:=2;
Cnt:=0;
while true do
begin
Inc(Cnt);
N1:=GetNextPrime(N);
FN:=N * N1 - N - N1;
N:=N1;
if FN>10000 then break;
Memo.Lines.Add(Format('%2d = %5d',[Cnt,FN]));
end;
end;
</syntaxhighlight>
{{out}}
<pre>
1 = 1
2 = 7
3 = 23
4 = 59
5 = 119
6 = 191
7 = 287
8 = 395
9 = 615
10 = 839
11 = 1079
12 = 1439
13 = 1679
14 = 1931
15 = 2391
16 = 3015
17 = 3479
18 = 3959
19 = 4619
20 = 5039
21 = 5615
22 = 6395
23 = 7215
24 = 8447
25 = 9599
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
fastfunc nextprim prim .
repeat
prim += 1
until isprim prim = 1
.
return prim
.
prim = 2
repeat
prim0 = prim
prim = nextprim prim
x = prim0 * prim - prim0 - prim
until x >= 10000
write x & " "
.
</syntaxhighlight>
{{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|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
"Frobenius numbers < 10,000:" print
Line 654 ⟶ 805:
[ nip dup next-prime ] [ * ] [ [ - ] dip - ] 2tri
dup 10,000 <
] [ . ] while 3drop</
{{out}}
<pre style="height:14em">
Line 686 ⟶ 837:
=={{header|Fermat}}==
<
for n = 1 to 25 do !!Frobenius(n) od</
{{out}}
<pre>
Line 718 ⟶ 869:
=={{header|FreeBASIC}}==
<
dim as integer pn=2, n=0, f
Line 729 ⟶ 880:
pn = i
end if
next i</
{{out}}
<pre>
Line 758 ⟶ 909:
25 9599
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
local fn IsPrime( n as long ) as BOOL
long i
BOOL result = YES
if ( n < 2 ) then result = NO : exit fn
for i = 2 to n + 1
if ( i * i <= n ) and ( n mod i == 0 )
result = NO : exit fn
end if
next
end fn = result
void local fn ListFrobenius( upperLimit as long )
long i, pn = 2, n = 0, f, r = 0
NSLog( @"Frobenius numbers through %ld:", upperLimit )
for i = 3 to upperLimit - 1 step 2
if ( fn IsPrime(i) )
n++
f = pn * i - pn - i
if ( f > upperLimit ) then break
NSLog( @"%7ld\b", f )
r++
if r mod 5 == 0 then NSLog( @"" )
pn = i
end if
next
end fn
fn ListFrobenius( 100000 )
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Frobenius numbers through 100000:
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
10199 10811 11447 12095 14111
16379 17679 18767 20423 22199
23399 25271 26891 28551 30615
32039 34199 36479 37631 38807
41579 46619 50171 51527 52895
55215 57119 59999 63999 67071
70215 72359 74519 77279 78959
82343 89351 94859 96719 98591
</pre>
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 787 ⟶ 995:
}
fmt.Printf("\n\n%d such numbers found.\n", len(frobenius))
}</
{{out}}
Line 800 ⟶ 1,008:
=={{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 809 ⟶ 1,017:
=={{header|J}}==
<syntaxhighlight lang
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>.)
{{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 816 ⟶ 1,027:
=={{header|Java}}==
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<
public static void main(String[] args) {
final int limit = 1000000;
Line 851 ⟶ 1,062:
return true;
}
}</
{{out}}
Line 884 ⟶ 1,095:
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 897 ⟶ 1,108:
.frob);
9999 | frobenius</
{{out}}
<pre>
Line 928 ⟶ 1,139:
=={{header|Julia}}==
<
const primeslt10k = primes(10000)
Line 952 ⟶ 1,163:
testfrobenius()
</
<pre>
Frobenius numbers less than 1,000,000 (an asterisk marks the prime ones).
Line 975 ⟶ 1,186:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
fn[n_] := Prime[n] Prime[n + 1] - Prime[n] - Prime[n + 1]
a = -1;
Line 985 ⟶ 1,196:
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 991 ⟶ 1,202:
=={{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,023 ⟶ 1,234:
var result = toSeq(frobenius(10_000))
echo "Found $1 Frobenius numbers less than $2:".format(result.len, N)
echo result.join(" ")</
{{out}}
Line 1,031 ⟶ 1,242:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,044 ⟶ 1,255:
# 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,052 ⟶ 1,263:
=={{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,068 ⟶ 1,279:
<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,077 ⟶ 1,288:
=={{header|Python}}==
<
#!/usr/bin/python
Line 1,110 ⟶ 1,321:
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,186 ⟶ 1,397:
END;
CALL EXIT;
EOF</
{{out}}
<pre>1
Line 1,216 ⟶ 1,427:
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
Procedure isPrime(v.i)
If v < = 1 : ProcedureReturn #False
Line 1,253 ⟶ 1,464:
CloseConsole()
End
</syntaxhighlight>
{{out}}
<pre>
Line 1,283 ⟶ 1,494:
</pre>
=={{header|Quackery}}==
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
In this solution the primes and Frobenius numbers are zero indexed rather than one indexed as per the task. It simplifies the code a smidgeon, as Quackery nests are zero indexed.
<syntaxhighlight lang="Quackery"> 200 eratosthenes
[ [ [] 200 times
[ i^ isprime if
[ i^ join ] ] ]
constant
swap peek ] is prime ( n --> n )
[ dup prime
swap 1+ prime
2dup * rot - swap - ] is frobenius ( n --> n )
[] 0
[ tuck frobenius dup
10000 < while
join swap
1+ again ]
drop nip echo </syntaxhighlight>
{{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|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,295 ⟶ 1,534:
=={{header|REXX}}==
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 10000 /* " " " " " " */
Line 1,333 ⟶ 1,572:
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,347 ⟶ 1,586:
=={{header|Ring}}==
<
? "working..." + nl + "Frobenius numbers are:"
Line 1,370 ⟶ 1,609:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</
{{out}}
<pre>working...
Line 1,383 ⟶ 1,622:
Found 25 Frobenius numbers
done...</pre>
=={{header|RPL}}==
« → max
« { } 2 OVER
'''DO'''
ROT SWAP + SWAP
DUP NEXTPRIME DUP2 * OVER - ROT -
'''UNTIL''' DUP max ≥ '''END'''
DROP2
» » ‘<span style="color:blue>FROB</span>’ STO
10000 <span style="color:blue>FROB</span>
{{out}}
<pre>
1: { 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|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
Prime.each_cons(2) do |p1, p2|
f = p1*p2-p1-p2
break if f > 10_000
puts f
end
</syntaxhighlight>
{{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|Rust}}==
<
// primal = "0.3"
Line 1,415 ⟶ 1,707:
}
println!();
}</
{{out}}
Line 1,440 ⟶ 1,732:
=={{header|Sidef}}==
<
prime(n) * prime(n+1) - prime(n) - prime(n+1)
}
Line 1,450 ⟶ 1,742:
take(n)
}
}</
{{out}}
<pre>
Line 1,458 ⟶ 1,750:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./
var primes = Int.primeSieve(101)
Line 1,472 ⟶ 1,762:
}
System.print("Frobenius numbers under 10,000:")
Fmt.tprint("$,5d", frobenius, 9)
System.print("\n%(frobenius.count) such numbers found.")</
{{out}}
Line 1,486 ⟶ 1,776:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,511 ⟶ 1,801:
Text(0, " Frobenius numbers found below 10,000.
");
]</
{{out}}
Line 1,523 ⟶ 1,813:
=={{header|Yabasic}}==
{{trans|PureBasic}}
<
sub isPrime(v)
if v < 2 then return False : fi
Line 1,547 ⟶ 1,837:
next i
end
</syntaxhighlight>
{{out}}
<pre>
|