Is Prime


This is a companion discussion topic for the original entry at https://toph.co/p/is-prime

pretests are so weak…

What’s the problem with this code? It gets wrong ans on test 6

N = int(input())


def isprime(n):
    if (n > 1):
        for i in range(2, n):
            if n % i == 0:
                return False
            else:
                return True
    else:
        return False


if isprime(N):
    print('Yes')
else:
    print('No')

@CSBMC_A You are returning True as soon as you find an i which cannot divide n without a remainder.

The loop should look something like this:

        for i in range(2, n):
            if n % i == 0:
                return False
        return True

#include <stdio.h>

int main() {
int n,c=0,i;
for(i=2;i<n;i++)
{ if(n%i==0)
c++;
break;
}
if(c==0)
{ printf(“Yes”);
}
else
{ printf(“No”);
}

return 0;

}
whats the problem ?

1 is not a prime number.your code takes 1 as prime

no;
it gets wrong answer in last test case

you have not even taking the inputs.
Your code is just printing YES maybe.
And the last TC might be NO.

According to your definition, the only prime number is 1 (which is ironic, since 1 is not actually prime). All other positive integers are divisible by (at least) themself and 1, making them non-prime according to your definition.

Agree with you. Using Fermat’s Little theorem i got accepted (Was not expecting though used just for testing purpose).

#include <stdio.h>
int main()
{
	int n,i,flag=0;
	scanf("%d",&n);
	if(n==1||n==0){
		printf("NO");
		}
    else{
	for(i=2;i<=n/2;i++){
		if(n%i==0)
			flag=1;
		}
		if(flag==0)
			printf("YES");
		else 
			printf("NO");
		}
return 0;
		}

Please help me
What is wrong in this

Hint: Every non-prime is divisible by 2 and/or 3… if a number is not divisible by either, the number is prime.

What’s the problem here?

#include <stdio.h>

int main()
{
	int n;
	int check = 0;
	printf("Enter a number:");
	scanf("%d", &n);
	for(int i = 2; i < n; i++){
		if(n % i == 0){
			check = 1;
			break;
		}
	}
	if(check == 0 && n != 1){
		printf("Yes");
	}
	else{
		printf("No");
	}
	return 0;
}

Remove extra output : “printf(“Enter a number:”);”

2 Likes

what is wrong in this

#include<iostream>

using namespace std;

int main(){

    int N;

    cin >> N;

        if(N%2!=0)

        {

            cout << "Yes" <<endl;

        }

        else

        {

            cout << "No" <<endl;

        }

        return 0;

    }

@Abdhulla.985108 You are checking if the code is odd or even. You are not checking if it is prime.

#include <stdio.h>
int main ()
{
    int N, i, k=0;

    scanf("%d", &N);
    if (N==1000)
        if ( N==1 || N==0)
            k=1;
    for(i=2; i<N; i++)
    {
        if(N%i==0)
            k++;
        break;
    }
    if(k==0)
        printf("\Yes");
    else
        printf("\No");

    return 0;
}

what is the problem, please help me