Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 1.04 KB

find_bug_1.md

File metadata and controls

57 lines (47 loc) · 1.04 KB

Find Bug 1

The following code has a bug. Can you find it?

The code is supposed to check if a number is prime.

C

int isPrime(int num) {
    int upperLimit = (int)(sqrt(num));
    int i;
    for (i = 2; i <= upperLimit; i++) {
	    if (i < num && num % i == 0) return 0;
	}
    return 1;
}

C++

int isPrime(int num) {
	int upperLimit = (int)(sqrt(num));
	for (int i = 2; i <= upperLimit; i++) {
		if (i < num && num % i == 0) return 0;
	}
	return 1;
}

Java

public int isPrime(int num) {
	int upperLimit = (int)(Math.sqrt(num));
	for (int i = 2; i <= upperLimit; i++) {
		if (i < num && num % i == 0) return 0;
	}
	return 1;
}

Python

def isPrime(self, num):
	upperLimit = int(num**0.5)
	for i in xrange(2, upperLimit + 1):
		if i < num and num % i == 0:
			return 0
	return 1

** Solutions coming up soon :)**