Modul:Benutzer/Neuneinhalbtel/MathUtils

Aus Wikibooks

Informationen zu dieser Dokumentation
Modul Benutzer

Modulbeschreibung

Module für Mathematikprobleme, speziell zum Erstellen von Mathematikaufgaben Verwendung in anderen Modulen

local mathUtils= require("Modul:Benutzer/Neuneinhalbtel/MathUtils")
r=mathUtils.randPicksInSet({2,3,5,7},3)

> {3,2,7}

Funktionen

randPicksInSet

Pick some random unique (or not unique) items of a set

randPicksInSet({2,3,5,7},3)

> {3,7,5}

randPicksInSet({2,3,5,7},3, true)
> {3,5,3}

nextCoprime

find coprime (de:teilerfremde) integers near a given integer

nextCoprime(10,{2,5,7})

> 9


Information


--- Module für Mathematikprobleme, speziell zum Erstellen von Mathematikaufgaben

-- Verwendung in anderen Modulen

--  local mathUtils= require("Modul:Benutzer/Neuneinhalbtel/MathUtils")
--  r=mathUtils.randPicksInSet({2,3,5,7},3) 
--> {3,2,7}
	
local p = {}
--- Pick some random unique (or not unique) items of a set
--  randPicksInSet({2,3,5,7},3) 
--> {3,7,5}
--  randPicksInSet({2,3,5,7},3, true)
--> {3,5,3}
function p.randPicksInSet(set, pickcount, notunique)
	pickcount=pickcount or 1
	--set=set or {6,7,8,9} --x
	local picks={}
	table.insert(picks, set[math.random(1,#set)])
	for i=2, pickcount do
		if not notunique then 
			for k,v in pairs(set) do
				if v==picks[#picks] then table.remove(set,k) end
			end
		end
		table.insert(picks, set[math.random(1,#set)])
	end
	return picks
end

--- find coprime (de:teilerfremde) integers near a given integer
--  nextCoprime(10,{2,5,7}) 
--> 9
function p.nextCoprime(n, ms, incr)    
    local newn, incr, nend
    if incr then inc=1; nend=n+50 else inc=-1; nend=1 end
    for testn = n,nend,inc do
   		local coll=0
    	for k,m in pairs(ms) do
    		if (math.mod(testn,m))==0 then 
    			coll=coll+1
	    	end
    	end
    	newn=testn
    	if coll==0 then	break end
    end
    return newn
end

return p