SamRob85
Badges: 0
Rep:
?
#1
Report Thread starter 6 years ago
#1
I was wondering if there was a simple way to see if a number is prime in python 3.3.3 or whether I need to do a big loop with decreasing numbers and check them against the original number.
0
reply
Damask-
Badges: 16
Rep:
?
#2
Report 6 years ago
#2
(Original post by SamRob85)
I was wondering if there was a simple way to see if a number is prime in python 3.3.3 or whether I need to do a big loop with decreasing numbers and check them against the original number.
If you were to find one, you would make a lot of money.

Check for factors between 2 and the (rounded up if necessary) square root of the number, and if it doesn't have any then you can be certain it's prime. This isn't feasible for huge numbers but I'm assuming you're not using huge numbers.
0
reply
SamRob85
Badges: 0
Rep:
?
#3
Report Thread starter 6 years ago
#3
(Original post by Damask-)
If you were to find one, you would make a lot of money.

Check for factors between 2 and the (rounded up if necessary) square root of the number, and if it doesn't have any then you can be certain it's prime. This isn't feasible for huge numbers but I'm assuming you're not using huge numbers.
Would I be able to miss out 6 if I did 3 as it is a multiple of it?
0
reply
Damask-
Badges: 16
Rep:
?
#4
Report 6 years ago
#4
(Original post by SamRob85)
Would I be able to miss out 6 if I did 3 as it is a multiple of it?
Sorry I thought I put this in my first post, check 2 outside the loop then for the loop make the 'step' 2 so it only checks odd numbers.
0
reply
Grecko
Badges: 1
Rep:
?
#5
Report 6 years ago
#5
(Original post by SamRob85)
I was wondering if there was a simple way to see if a number is prime in python 3.3.3 or whether I need to do a big loop with decreasing numbers and check them against the original number.
You would need to determine if a positive integer , is a prime number if it is not evenly divisible by any other integers other than 1 and itself. I stopped learning python for the time being so I cant show you an example based off the language but I can give you understanding of how algorithm should have to function.

Code:
// Program to generate a table of prime numbers

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])

{

@autoreleasepool {

int p, d, isPrime;

for ( p = 2; p <= 50; ++p ) {

isPrime = 1;

for ( d = 2; d < p; ++d )

if ( p % d == 0 )

isPrime = 0;

if ( isPrime != 0 )

scanf ("%i ", p);

}
}
return 0;
}
0
reply
Damask-
Badges: 16
Rep:
?
#6
Report 6 years ago
#6
(Original post by Grecko)
You would need to determine if a positive integer , is a prime number if it is not evenly divisible by any other integers other than 1 and itself. I stopped learning python for the time being so I cant show you an example based off the language but I can give you understanding of how algorithm should have to function.

Code:
// Program to generate a table of prime numbers

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])

{

@autoreleasepool {

int p, d, isPrime;

for ( p = 2; p <= 50; ++p ) {

isPrime = 1;

for ( d = 2; d < p; ++d )

if ( p % d == 0 )

isPrime = 0;

if ( isPrime != 0 )

scanf ("%i ", p);

}
}
return 0;
}
OP didn't want to generate primes, he wanted an algorithm to determine whether a given number was prime.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (644)
33.49%
Yes, I like the idea of applying to uni after I received my grades (PQA) (815)
42.38%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (378)
19.66%
I think there is a better option than the ones suggested (let us know in the thread!) (86)
4.47%

Watched Threads

View All