Tabliczka mnożenia
x=11:20
y=matrix(0,10,10)
for (i in x) {
for (j in x) {
y[i-10,j-10]=i*j
}
}
n_w=paste(x,"*",sep=""); n_k=paste("*",x,sep="")
colnames(y)=n_k; rownames(y)=n_w
print(y)
######################################
x=11:20
y=matrix(x,10,10)
z=y*t(y)
n_w=paste(x,"*",sep=""); n_k=paste("*",x,sep="")
colnames(z)=n_k; rownames(z)=n_w
print(z)
######################################
x=matrix(11:20,10,1)
y=x%*%t(x)
n_w=paste(x,"*",sep=""); n_k=paste("*",x,sep="")
colnames(y)=n_k; rownames(y)=n_w
print(y)
ćwiczenia 2
kg=function(A){
Aa=rbind(0,cbind(0,A,0),0)
ls=Aa[2:(m+1),1:n]+Aa[2:(m+1),3:(n+2)]+
Aa[1:m,2:(n+1)]+Aa[3:(m+2),2:(n+1)]
B=(ls==3)+((ls==2)*A)
return(B)
}
m=7;n=7
A=matrix(round(runif(m*n)),m,n)
mp=array(A,c(m,n,1))
dalej=T;i=1
while (dalej) {
mw=mp[,,i]
ng=kg(mw)
if (sum(ng)==0) {dalej=F;koniec='wyzerowana'}
if (sum(mw==ng)==(m*n)) {dalej=F;koniec='stabilna'}
if (i>1) {
for (j in 1:(i-1)) {
if (sum(mp[,,j]==ng)==(m*n)) {dalej=F;koniec='okresowa'}
}
}
i=i+1
mp=array(c(mp,ng),c(m,n,i))
}
print(paste('koniec po',i,'krokach, tablica',koniec))
Ćwiczenie 3
gk=function(x,m=10,s=4){
return(floor(x^2*m^(-s/2))-floor(x^2*m^(-3*s/2))*m^s)
}
genk=function(x,m=10,s=4){
knt=T
while (knt) {
xn=gk(x[length(x)],m,s)
knt=(sum(x==xn)==0)
x=c(x,xn)
}
return(x[-length(x)])
}
m=10
s=2
dlg=vector()
for (i in 1:(m^s)) {
dlg[i]=length(genk(i-1,m,s))
}
sr=mean(dlg)
mx=max(dlg)
v=(dlg==mx)*(1:length(dlg))
najd=v[v!=0]-1
print(c(sr,mx,najd))
Ćwiczenie 4 fibon
fibon=function(n=1,x_0=13,x_1=7,m=2^10)
{
x=c(x_0,x_1)
for (i in 3 :(n+2)) x[i]=(x[i-1]+x[i-2])%%m
return(x[-(1:2)])
}
Ćwiczenie 5 generator
gk=function(x,m=10,s=4){
return(floor(x^2*m^(-s/2))-floor(x^2*m^(-3*s/2))*m^s)
}
genk=function(x,m=10,s=4){
knt=T
while (knt) {
xn=gk(x[length(x)],m,s)
knt=(sum(x==xn)==0)
x=c(x,xn)
}
return(x[-length(x)])
}
m=10
s=4
Ćwiczenie 6 multigenerator
multi=function(n=1,a=13, x=7,m=2^10)
{
for (i in 2 :(n+1)) x[i]=(x[i-1]*a)%%m
return(x[-1])
}
Ćwiczenie 7
op=function(x,y=7654321/16777216,typ=F){
s=x-y+(x<y)-3/16777216*typ
return(s)
}
V<- function(n=1){
x=runif(97)
for (i in 98:(97+n)) x[i]=op(x[i-97],x[i-33],F)
return(x[-(1:97)])
}
c<- function(n=1){
x=runif(1)
for (i in 2:(1+n)) x[i]=op(x[i-1])
return(x[-1])
}
u=function(n=1) {
return(op(v(n),c(n),F))
}
Ćwiczenie 8 serwer
wt=function (t){
h=floor(t/60)
m=floor(t%%60)
s=round((t%%1)*60,0)
return(paste(h,"h",m,"m",s,"s"))
}
czas=24*60;lambda=200;lmn=0;lsd=2
n=rpois(1,lambda)
cz_w=runif(n,0,czas);cz_w=cz_w[order(cz_w)]
cz_r=rlnorm(n,0,2)
s_z=cz_w; k_z=cz_w+cz_r
for (i in 2:n) {
if (cz_w[i]<k_z[i-1]) {
s_z[i]=k_z[i-1]
k_z[i]=s_z[i]+cz_r[i]
}
}
wolne=sum(c(s_z,czas)-c(0,k_z))
prpo=k_z[n]-czas
if (prpo<=0) prpo=0 else wolne=wolne-(czas-k_z[n])
komunikat=rbind(paste("ilość zadań czekających ",sum(cz_w<s_z),"na",n),
paste("maksymalnie czekania ",wt(max(s_z-cz_w))),
paste("średni czas czekania ",wt(mean(s_z-cz_w)*n/sum(cz_w<s_z))),
paste("średni czas realizacji ",wt(mean(k_z-cz_w))),
paste("czas pracy serwera po 24h",wt(prpo)),
paste("czasu wolnego serwera ",wt(wolne)))
print(komunikat)