Alternative to goto in c# -
i given program employer, , asked find out wrong it. have heard bad things goto's, , not have experience them whatsoever. know how reprogram without goto's? absolute mess...
class goto { public int solve(int limit) { int result = 1; list num = new list(); list usedprimes = new list();
alpha: int = 2; if (isprime(i)) { num.add(i); } i++; if (i <= limit) goto alpha; result = 1; beta: if (num.count == 0) goto gamma; int upper, value; int prime = num[0]; //system.out.println(prime); upper = (limit - 1) / prime; value = upper * prime; if (!(isprime(upper) && upper != 1)) goto epsilon; //addusedprime(upper, usedprimes); deletefromlist(upper, num); goto phi; epsilon: deletefromlist(prime, num); phi: if (checkfactors(value, usedprimes)) goto zeta; result += value; console.writeline(prime + ", " + upper + ", " + value + ", " + result); addusedprime(prime, usedprimes); (int j = 2; j <= value; j++) { if ((value % j == 0) && isprime(j)) { //system.out.println("adding used prime: " + j); addusedprime(j, usedprimes); } } goto iota; zeta: if (!checkfactors(prime, usedprimes)) { result += prime; addusedprime(prime, usedprimes); console.writeline(prime + ", " + upper + ", " + value + ", " + result + " (grace)"); deletefromlist(prime, num); } iota: goto beta; gamma: return result; } private void deletefromlist(int prime, list<int> num) { (int = 0; < num.count; i++) { if (prime == num[i]) { num.remove(i); break; } } } private void addusedprime(int prime, list<int> usedprimes) { int i; = 0; alpha: if (prime == usedprimes[i]) { goto gamma; } i++; if (i < usedprimes.count) goto alpha; gamma: if (i == usedprimes.count) { usedprimes.add(prime); } } private boolean checkfactors(int num, list<int> primes) { boolean result = false; int = 0; epsilon: if (num % primes[i] == 0) { result = true; goto delta; } i++; if (i < primes.count) goto epsilon; delta: return result; } private boolean isprime(int num) { boolean result = true; if (num == 2 || num == 1) return true; int limit = (int)math.sqrt(num); int = 2; epsilon: if (num % == 0) { result = false; goto delta;} i++; if (i <= limit) goto epsilon; delta: return result; } }
Comments
Post a Comment