+ 1

How to check if given no is prime?

30th Jul 2017, 8:05 AM
S S singh
S S singh - avatar
4 odpowiedzi
30th Jul 2017, 8:43 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 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
27th Jul 2018, 4:40 AM
Maz
Maz - avatar
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
30th Jul 2017, 9:59 AM
Maydo
Maydo - avatar
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
27th Jul 2018, 11:50 AM
Flandre Scarlet
Flandre Scarlet - avatar