Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,41 @@
BEGIN
INT examples=10, classes=5;
MODE SEMIPRIME = STRUCT ([examples]INT data, INT count);
[classes]SEMIPRIME semi primes;
PROC num facs = (INT n) INT :
COMMENT
Return number of not necessarily distinct prime factors of n.
Not very efficient for large n ...
COMMENT
BEGIN
INT tf := 2, residue := n, count := 1;
WHILE tf < residue DO
INT remainder = residue MOD tf;
( remainder = 0 | count +:= 1; residue %:= tf | tf +:= 1 )
OD;
count
END;
PROC update table = (REF []SEMIPRIME table, INT i) BOOL :
COMMENT
Add i to the appropriate row of the table, if any, unless that row
is already full. Return a BOOL which is TRUE when all of the table
is full.
COMMENT
BEGIN
INT k := num facs(i);
IF k <= classes
THEN
INT c = 1 + count OF table[k];
( c <= examples | (data OF table[k])[c] := i; count OF table[k] := c )
FI;
INT sum := 0;
FOR i TO classes DO sum +:= count OF table[i] OD;
sum < classes * examples
END;
FOR i TO classes DO count OF semi primes[i] := 0 OD;
FOR i FROM 2 WHILE update table (semi primes, i) DO SKIP OD;
FOR i TO classes
DO
printf (($"k = ", d, ":", n(examples)(xg(0))l$, i, data OF semi primes[i]))
OD
END

View file

@ -0,0 +1,6 @@
1>::48*"= k",,,,02p.":",01v
|^ v0!`\*:g40:<p402p300:+1<
K| >2g03g`*#v_ 1`03g+02g->|
F@>/03g1+03p>vpv+1\.:,*48 <
P#|!\g40%g40:<4>:9`>#v_\1^|
|^>#!1#`+#50#:^#+1,+5>#5$<|

View file

@ -0,0 +1,23 @@
defmodule Factors do
def factors(n), do: factors(n,2,[])
defp factors(1,_,acc), do: acc
defp factors(n,k,acc) when rem(n,k)==0, do: factors(div(n,k),k,[k|acc])
defp factors(n,k,acc) , do: factors(n,k+1,acc)
def kfactors(n,k), do: kfactors(n,k,1,1,[])
defp kfactors(_tn,tk,_n,k,_acc) when k == tk+1, do: IO.puts "done! "
defp kfactors(tn,tk,_n,k,acc) when length(acc) == tn do
IO.puts "K: #{k} #{inspect acc}"
kfactors(tn,tk,2,k+1,[])
end
defp kfactors(tn,tk,n,k,acc) do
case length(factors(n)) do
^k -> kfactors(tn,tk,n+1,k,acc++[n])
_ -> kfactors(tn,tk,n+1,k,acc)
end
end
end
Factors.kfactors(10,5)

View file

@ -0,0 +1,24 @@
-module(factors).
-export([factors/1,kfactors/0,kfactors/2]).
factors(N) ->
factors(N,2,[]).
factors(1,_,Acc) -> Acc;
factors(N,K,Acc) when N rem K == 0 ->
factors(N div K,K, [K|Acc]);
factors(N,K,Acc) ->
factors(N,K+1,Acc).
kfactors() -> kfactors(10,5,1,1,[]).
kfactors(N,K) -> kfactors(N,K,1,1,[]).
kfactors(_Tn,Tk,_N,K,_Acc) when K == Tk+1 -> io:fwrite("Done! ");
kfactors(Tn,Tk,N,K,Acc) when length(Acc) == Tn ->
io:format("K: ~w ~w ~n", [K, Acc]),
kfactors(Tn,Tk,2,K+1,[]);
kfactors(Tn,Tk,N,K,Acc) ->
case length(factors(N)) of K ->
kfactors(Tn,Tk, N+1,K, Acc ++ [ N ] );
_ ->
kfactors(Tn,Tk, N+1,K, Acc) end.

View file

@ -0,0 +1,17 @@
for k = 1 to 5
{
n=2
count = 0
print["k=$k:"]
do
{
if length[factorFlat[n]] == k
{
print[" $n"]
count = count + 1
}
n = n + 1
} while count < 10
println[]
}

View file

@ -0,0 +1,27 @@
primes = 2:3:[n | n <- [5,7..], foldr (\p r-> p*p > n || rem n p > 0 && r)
True (drop 1 primes)]
merge aa@(a:as) bb@(b:bs)
| a < b = a:merge as bb
| otherwise = b:merge aa bs
-- n-th item is all k-primes not divisible by any of the first n primes
notdivs k = f primes $ kprimes (k-1) where
f (p:ps) s = map (p*) s : f ps (filter ((/=0).(`mod`p)) s)
kprimes k
| k == 1 = primes
| otherwise = f (head ndk) (tail ndk) (tail $ map (^k) primes) where
ndk = notdivs k
-- tt is the thresholds for merging in next sequence
-- it is equal to "map head seqs", but don't do that
f aa@(a:as) seqs tt@(t:ts)
| a < t = a : f as seqs tt
| otherwise = f (merge aa $ head seqs) (tail seqs) ts
main = do
-- next line is for task requirement:
mapM_ (\x->print (x, take 10 $ kprimes x)) [1 .. 5]
putStrLn "\n10000th to 10100th 500-amost primes:"
mapM_ print $ take 100 $ drop 10000 $ kprimes 500

View file

@ -0,0 +1,28 @@
public class AlmostPrime
{
public static void main(String args[])
{
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
}
System.out.println("");
}
}
public static boolean kprime(int n, int k)
{
int f = 0;
for (int p = 2; f < k && p*p <= n; p++)
while (0 == n % p){
n /= p;
f++;
}
return f + ((n > 1)?1:0) == k;
}
}

View file

@ -0,0 +1,28 @@
/*REXX program computes & displays N numbers of the first K k─almost primes.*/
parse arg N K . /*get optional arguments from the C.L. */
if N=='' then N=10 /*N not specified? Then use default.*/
if K=='' then K= 5; w=length(k) /*K " " " " " */
/*W: is the width of K, used for output*/
do m=1 for K; $=2**m; fir=$ /*generate & assign 1st k─almost prime.*/
#=1; if #==N then leave /*#: k─almost primes; Enough are found?*/
#=2; $=$ 3*(2**(m-1)) /*generate & append 2nd k─almost prime.*/
do j=fir+fir+1 until #==N /*process an almost-prime N times.*/
if #factr(j)\==m then iterate /*not the correct k─almost prime? */
#=#+1; $=$ j /*bump K─almost counter; append it to $*/
end /*j*/ /* [↑] generate N k─almost primes.*/
say right(m,w)"─almost ("N') primes:' $ /*displays " " " " */
end /*m*/ /* [↑] display a line for each K-prime*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
#factr: procedure; parse arg x 1 z /*defines X and Z to the argument. */
do f=0 while z//2==0; z=z%2; end /*÷ by 2s.*/
do f=f while z//3==0; z=z%3; end /*÷ " 3s.*/
do f=f while z//5==0; z=z%5; end /*÷ " 5s.*/
j=5
do y=0 by 2; j=j+2+y//4 /*insure J isn't divisible by three. */
parse var j '' -1 _ /*obtain the right─most decimal digit. */
if _==5 then iterate /*fast check for divisible by five. */
if j>z then leave /*is number reduced to the smallest # ?*/
do f=f+1 while z//j==0; z=z%j; end; f=f-1 /*÷ by Js.*/
end /*y*/ /* [↑] find all the factors in X. */
return max(f,1) /*if prime (f==0), then return 1. */

View file

@ -1,39 +1,39 @@
fn is_kprime(n: usize, k: usize) -> bool {
let mut primes = 0us;
let mut f = 2us;
let mut rem = n;
while primes < k && rem > 1{
while (rem % f) == 0 && rem > 1{
rem /= f;
primes += 1;
}
f += 1;
}
rem == 1 && primes == k
fn is_kprime(n: u32, k: u32) -> bool {
let mut primes = 0;
let mut f = 2;
let mut rem = n;
while primes < k && rem > 1{
while (rem % f) == 0 && rem > 1{
rem /= f;
primes += 1;
}
f += 1;
}
rem == 1 && primes == k
}
struct KPrimeGen {
k: usize,
n: usize,
k: u32,
n: u32,
}
impl Iterator for KPrimeGen {
type Item = usize;
fn next(&mut self) -> Option<usize> {
self.n += 1;
while !is_kprime(self.n, self.k) {
self.n += 1;
}
Some(self.n)
}
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.n += 1;
while !is_kprime(self.n, self.k) {
self.n += 1;
}
Some(self.n)
}
}
fn kprime_generator(k: usize) -> KPrimeGen {
KPrimeGen {k: k, n: 1}
fn kprime_generator(k: u32) -> KPrimeGen {
KPrimeGen {k: k, n: 1}
}
fn main() {
for k in 1us..6 {
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
for k in 1..6 {
println!("{}: {:?}", k, kprime_generator(k).take(10).collect::<Vec<_>>());
}
}

View file

@ -0,0 +1,54 @@
For k = 1 To 5
count = 0
increment = 1
WScript.StdOut.Write "K" & k & ": "
Do Until count = 10
If PrimeFactors(increment) = k Then
WScript.StdOut.Write increment & " "
count = count + 1
End If
increment = increment + 1
Loop
WScript.StdOut.WriteLine
Next
Function PrimeFactors(n)
PrimeFactors = 0
arrP = Split(ListPrimes(n)," ")
divnum = n
Do Until divnum = 1
For i = 0 To UBound(arrP)-1
If divnum = 1 Then
Exit For
ElseIf divnum Mod arrP(i) = 0 Then
divnum = divnum/arrP(i)
PrimeFactors = PrimeFactors + 1
End If
Next
Loop
End Function
Function IsPrime(n)
If n = 2 Then
IsPrime = True
ElseIf n <= 1 Or n Mod 2 = 0 Then
IsPrime = False
Else
IsPrime = True
For i = 3 To Int(Sqr(n)) Step 2
If n Mod i = 0 Then
IsPrime = False
Exit For
End If
Next
End If
End Function
Function ListPrimes(n)
ListPrimes = ""
For i = 1 To n
If IsPrime(i) Then
ListPrimes = ListPrimes & i & " "
End If
Next
End Function