[R] function stays in loop

Rui Barradas ruipbarradas at sapo.pt
Mon Dec 3 00:50:12 CET 2012


Hello,

Because p/i == 4/2 == 2 thenyou subtract 1 unil q == 0 and i = i+1 is 
never executed.
(Use print statements to check it.)

Hope this helps,

Rui Barradas

Em 02-12-2012 11:57, Mkkl escreveu:
> function <- function(p){
> i <- 2
> r <- 0
> while(i < p) {
> 	q <- (p/i)
> 		while(q>=1) {
> 			q=(q-1)
> }
> 		if (q==0) {
> 			r=1
> } 		else
> 			i=i+1
> }
> return(r)
> }
>
> This function stays in some kind of loop when I enter 4 as a function value.
> It immediately returns 0 when I inter 3 though.
> I don't seem te get why it does not give me just a result. Can someone tell
> me why it stays in a loop?
>
> Kind regards,
>
> Michiel
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/function-stays-in-loop-tp4651619.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list