The simulated price i got for the american option is lower than the european. Can i anybody help me see where is the problem?

My program looks like this, and I'm using the program R

n <- 3 #antal af exercise points

N <- 10000 #numbers of simulated paths%

K <- 30 ##strike price

r <- 0.05 #interest rate

T <- 1 #time

sigma <- 0.4 #volatility

S0 <-36 #asset price

randn <- matrix(rnorm(n*N),N, n)

dt <- T/n

disc <- exp(-r*dt)

dX <- sqrt(dt)*randn

#Simulated paths

S <- matrix(ncol=n, nrow=N)

for (i in 2:n){

for (j in 1:N){

S[j,1] <- S0*exp((r-1/2*sigma^2)*dt+sigma*dX[j,1])

S[j,i] <- S[j,i-1]*exp((r-1/2*sigma^2)*dt+sigma*dX[j,i])

}

}

#Cashflow-matrix#

f <- function(x){max(K-x,0)}

P <- matrix(data=NA, N, n)

for (j in n:1) {

for (i in 1:N) {

P[i,j] <- f(S[i,j])

}

}

#Basis functions#

f1 <- function(x){exp(-x/2)}

f2 <- function(x){exp(-x/2)*(1-x)}

f3 <- function(x){exp(-x/2)*(1-2*x+x^2/2)}

X <- matrix(data=NA, N, 1)

Y <- matrix(data=NA, N, 1)

for (i in (n-1):1){

for (j in 1:N){

if (P[j,i] > 0) {

X[j,1] <- S[j,i]

Y[j,1] <- disc*P[j,i+1]

}

else{

Y[j,1] <- 0

X[j,1] <- 0

}

}

B1 <- ifelse(f1(X)[,1]==1,0,f1(X)[,1])

B2 <- ifelse(f2(X)[,1]==1,0,f2(X)[,1])

B3 <- ifelse(f3(X)[,1]==1,0,f3(X)[,1])

R <- cbind(Y,B1,B2,B3)

index <- which(P[,i]==0)

reg <- data.frame(R[-index,])

Beta <- lm(V1 ~ B1 + B2+ B3, data=reg)$coefficients

C <- ifelse(X==0,0, rep(Beta[1],N)+Beta[2]*R[,2] + Beta[3]*R[,3] + Beta[4]*R[,4])

for (j in 1:N){

if (C[j] < P[j,i]){

P[j,(i+1):n] <- 0

}

else{

P[j,i] <- 0

}

}

}

Z <- matrix(data=NA, N, n)

g <- function(x){x*disc^j}

for (i in 1:N){

for (j in 1:n){

Z[i,j] <- g(P[i,j])

}

}

AMPrice <- sum(Z)/N

AMPrice