+ 1
How to check if given no is prime?
4 Réponses
+ 6
-------- isPrime.s --------
.globl _isPrime
_isPrime:
pushl %ebx
pushl %esi
pushl %edi
movl 16(%esp), %ecx
movl $1, %esi
movl %ecx, %edi
subl $1, %edi
P: cmpl $2, %edi
jl Z
xorl %edx, %edx
movl %ecx, %eax
movl %edi, %ebx
idiv %ebx
cmpl $0, %edx
jne K
xorl %esi, %esi
K: decl %edi
jmp P
Z: movl %esi, %eax
popl %edi
popl %esi
popl %ebx
ret
-------- main.c --------
#include <stdio.h>
int isPrime(int);
int main(void)
{
int n = 7;
if(isPrime(n) == 0) printf("%d%s\n", n, "is composite");
else printf("%d%s\n", n, "is prime");
return 0;
}
To compile it on Windows, (make sure GCC is in your PATH)
$ gcc -g -m32 main.c isPrime.s -o main
0
Code above ("Find Number is Prime or not") is not quite correct and a bit too slow.
https://code.sololearn.com/c658qToiC3rq/?ref=app
0
based on a simple rule:
except for 2 and 3, all prime numbers are 6n±1
so we can do this: (python)
if num < 4:
return num > 1
elif num%6 not in [1,5]:
return False
else:
p = 1
while (6*p+1)**2 <= num:
if num%(6*p-1) and num%(6*p+1):
p += 1
else:
return False
return True
here, i write it and also show its time
https://code.sololearn.com/cj5zw7ohTv4L