vb.net - Collection Was Modified; Enumeration Operation May Not Continue -


i ran code , error came out: collection modified; enumeration operation may not execute. highlighted line:

next 

here's code:

dim maxnum integer = 100         dim primelist new generic.list(of integer)         primelist.add(2)           prime integer = 3 maxnum step 1             each element string in primelist                 if prime mod element = 0                  else                     primelist.add(prime)                 end if             next         next 

would me, please?

great thanks!

if search, find other ways of checking prime numbers, method fine if want find primes between 2 , 100.

if going loop through elements in primelist, need avoid updating primelist inside loop. in case, can't decide number prime until have checked possible divisors.

the following code uses boolean keep track if divisors found.

dim maxnum integer = 100 dim primelist new generic.list(of integer) primelist.add(2)  prime integer = 3 maxnum step 1     dim isprime boolean = true     each element string in primelist         if prime mod element = 0 isprime = false     next     if isprime primelist.add(prime) next 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -