+ 3
I've written a code that outputs even and odd numbers from 1 to 20, i need help in writing prime numbers, anybody aware?
Prime numbers between 1 and 20, C
21 Respostas
+ 10
//prime numbers from 1 to n in C
int n = 20;
for (int i =2;i<n;i++){
int isprime=1;
for (int j=2;j<i;j++){
if (i!=j && i%j==0)
isprime=0;
}
if(isprime) printf("%d ",i);
}
+ 9
You can search Code Playground for examples and inspirations š
+ 2
i made a program once to calculate prime numbers. basically, i had a āprimesā list with just the number 2 in it, and then i started at 3 and went through all the numbers up to 20 (or whatever). on each number, i checked through all the numbers currently in āprimesā to see if it was integer divisible by any of them. if it was, i broke the loop and moved on. if it made it through, it got appended to the primes list.
+ 1
Thank you, but I'm afraid that doesn't satisfy my question...
+ 1
Sorry about that, but usually we search Code Playground for codes, and here in Q&A forum we ask questions.
+ 1
It got it, thank you...
+ 1
AssasinStudent that code works :-)
+ 1
Prime numbers
Only 2 is the even prime number so it needs to be included.
From odd prime number only those are primes whose factors are integer apart from 1 and odd number it self.
9 is not prime it has integer factor 3 apart from 1 and 9
15 is not prime it has integer factor 3 and 5 apart from 1 and 15.
DHANANJAY
+ 1
ā¦ I did not understand that. Are you trying to define prime numbers, or is this actually a formula for getting prime numbers more easily than weāve been suggesting? Iāve been wondering if there was such a formula for a long time.
+ 1
Wilbur Jaywright
I believe this is what he's talking about.
https://code.sololearn.com/cw3lO1sgEYGP/?ref=app
+ 1
Which languange specifically Ochieng?
I think concept on modulus can serve it bestš
+ 1
hey, what do you know? had it on my phone! good oleā program š. https://code.sololearn.com/cVc3Vay3utXI/?ref=app
+ 1
@deleted
you can make your code faster using the observation that the highest divisor of a number besides itself is smaller or equal to the square root of the number. In your code you can replace:
for (int j=2;j<i;j++){
with
for (int j=2; j*j<=i;j++){
+ 1
Eddy M you just explained the theory to me. š
+ 1
@Wilbur Jaywright I'm happy if you found it useful.
0
Tabular method is better than
1 x 1 => 1
3 x 1 => 3
7 x 1 => 7
9 x 1 => 9
1 x 3 => 3
3 x 3 => 9
7 x 3 => 1
9 x 3 => 7
1 x 7 => 7
3 x 7 => 1
7 x 7 => 9
9 x 7 => 3
1 x 9 => 9
3 x 9 => 7
7 x 9 => 3
9 x 9 => 1
After 1, 2, 5 as prime numbers
Last digits for decimal primes are 1, 3, 7 and 9
It can be used for big primeness search
DHANANJAY
0
35 will return as prime with that code. :-(
0
Philip Ochieng
If small prime needs with range the Tabular method is good
Table needs to be made using digit sum and last digit
1 3 7 9
1
2
3
4
5
6
7
8
9
columns are last digit
And row says the digit some for each round of numbers
table needs to be prepared ones and used till application is running.
DHANANJAY
- 1
Use the c language