Original Post
🔒 Locked
prime and %
Advertisement
int iNum = 0;
scanf("%d",&iNum);
bool bPrime = true;
for(int i=2;i {
if(iNum%i==0){ bPrime = false; break;}
}
I dont know if it works lol im at work.
scanf("%d",&iNum);
bool bPrime = true;
for(int i=2;i
if(iNum%i==0){ bPrime = false; break;}
}
I dont know if it works lol im at work.
Here's some Scheme code I made a while ago (with some slight modifications), that will check if a number is prime:
(define (divisible? a b) (= (modulo a b) 0))(define (any f xs) (cond ((null? xs) #f) ((f (car xs)) #t) (else (any f (cdr xs))))); Find all primes less than n.(define (primes n) (define (primes-loop n max primes) (if (>= n max) primes (if (any (lambda (a) (divisible? n a)) primes) (primes-loop (+ n 1) max primes) (primes-loop (+ n 1) max (cons n primes))))) (primes-loop 3 n '(2)))(define (in x xs) (any (lambda (y) (equal? x y)) xs))(let ((num (read))) (if (in num (primes (+ num 1))) (display "The given number is prime.") (display "The given number is not prime.")) (newline))
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement