+ 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

15th Mar 2021, 4:46 AM
Philip Ochieng
Philip Ochieng - avatar
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); }
15th Mar 2021, 5:12 AM
deleted
+ 9
You can search Code Playground for examples and inspirations šŸ‘
15th Mar 2021, 4:57 AM
Ipang
+ 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.
15th Mar 2021, 5:12 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Thank you, but I'm afraid that doesn't satisfy my question...
15th Mar 2021, 4:59 AM
Philip Ochieng
Philip Ochieng - avatar
+ 1
Sorry about that, but usually we search Code Playground for codes, and here in Q&A forum we ask questions.
15th Mar 2021, 5:01 AM
Ipang
+ 1
It got it, thank you...
15th Mar 2021, 5:09 AM
Philip Ochieng
Philip Ochieng - avatar
+ 1
AssasinStudent that code works :-)
15th Mar 2021, 5:23 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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
16th Mar 2021, 12:33 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 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.
16th Mar 2021, 1:49 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Wilbur Jaywright I believe this is what he's talking about. https://code.sololearn.com/cw3lO1sgEYGP/?ref=app
16th Mar 2021, 2:30 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Which languange specifically Ochieng? I think concept on modulus can serve it bestšŸ˜Š
16th Mar 2021, 10:12 AM
Were Vincente'
Were Vincente' - avatar
+ 1
hey, what do you know? had it on my phone! good oleā€™ program šŸ˜Š. https://code.sololearn.com/cVc3Vay3utXI/?ref=app
16th Mar 2021, 3:00 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 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++){
17th Mar 2021, 12:24 AM
Eddy M
+ 1
Eddy M you just explained the theory to me. šŸ˜„
17th Mar 2021, 12:32 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
@Wilbur Jaywright I'm happy if you found it useful.
17th Mar 2021, 12:42 AM
Eddy M
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
16th Mar 2021, 2:43 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
35 will return as prime with that code. :-(
16th Mar 2021, 11:55 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
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
17th Mar 2021, 3:18 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
- 1
Use the c language
17th Mar 2021, 2:03 AM
Saikou