Program wont loop, Prime Number finder, C - c

I just started learning C and i would like to make a program that finds how many prime numbers are within 200 and 300, but i dont seem to get it right as my program seems not to even loop. Could you suggest a fix? For those who dont know, prime numbers are those greater than 1 that cannot be formed by multiplying two smaller natural numbers. (ex. 3, 5, 7)
#include <stdio.h>
#define START 200
#define END 300
int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
for (j = 1; j <= i; j++)
{
if (i%j == 0)
{
c++;
}
if (c == 2)
{
primenum = primenum + 1;
}
}
}
printf("tHE PRIME NUMBERS ARE %d", primenum);
}

This is my solution:
#include <stdio.h>
#define START 200
#define END 300
int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{
c++;
}
}
if (c == 2) primenum = primenum + 1;
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}
I put c = 2 in the for with i because a prime number has 2 divisor (1 and itself). The for with j starts from 2 because 1 is a divisor and ends at i-1 because i is the number and each number is a divisor for itself. I tested the c value at the end of the for with j because if this value is tested inside the result is wrong. I obtained 16 prime numbers between 200 and 300.

Let's fix some issues.
for (j = 1; j <= i; j++) Every number is divisible by 1 and
itself so you need to fix this issue. You should start j=2 to
j=i-1 You need to change for (j = 1; j <= i; j++) to for (j = 2;
j <i; j++)
You need to reset the counter variable c for every number before you
enter the nested for loop.
You're checking the value of c in the loop and that's why you're
getting the wrong result. You should check the value of c after
breaking out from the loop.
#include <stdio.h>
#define START 100
#define END 200
int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
c=2;
for (j = 2; j <i; j++)
{
if (i%j == 0)
{
c++;
break;
}
}
if (c == 2)
{
primenum = primenum + 1;
}
}
printf("tHE PRIME NUMBERS ARE %d", primenum);
}

Related

how to list all pairs of sexy primes

How can i write a program that lists all sexy prime pairs that exist in n numbers.
For example if n = 10 the output should be (5, 11) and (7, 13)
My idea was to generate all primes within n and then add 6 to each and check if the i + 6 is a prime. But it doesnt work, there's no output and the program ends.
#include <stdio.h>
int main() {
int i, j, n, k, isprime = 1, prime2, flag = 0;
scanf("%d", &n);
for (i = 3; i <= n; i++){
for (j = 2; j <= i; j++){
if (i % j == 0)
break;
}
if (i == j){
prime2 = i + 6;
for (k = 3; k <= prime2; k++){
if (prime2 % k == 0){
flag++;
break;
}
}
if (flag == 0){
printf("%d %d\n", i, prime2);
}
}
}
return 0;
}
Any ideas of what im doing wrong or any tips on how to solve it? (with loops only)
As there're a lot of resources about finding a prime number, I'm not going to discuss that. Rather I'll try to point out the bug in your code.
First problem:
for (k = 3; k <= prime2; k++)
Here you need to run the loop till prime2 - 1. Also you should start checking from 2 rather than 3, just like you did previously. That means,
for (k = 2; k < prime2; k++)
or
for (k = 2; k <= prime2 - 1; k++)
Reason: when k = prime2, prime2 % k will be 0. For finding out whether a number is prime we don't need to check if that number is divisible by 1 and that number itself.
Note: Now you might think why the first prime number loop for (j = 2; j <= i; j++) is working .
It's working because you've given an additional condition if (i == j) after it.
Second problem:
You need to declare the flag variable within the first loop.
for (i = 2; i <= n; i++)
{
int flag = 0;
.... (rest of the code)
....
}
Reason: Basically with the flag value, you're trying to find out whether prime2 is a prime number.
Every time you'll get a prime number from the first loop, you'll have a new value of prime2. In your code, once you're incrementing the value of flag, you're never resetting the flag value.
That's why once your code detects a prime2 which is not a prime, it'll never detect the second prime number again (prime2 which is actually prime).
Overall code:
#include <stdio.h>
int main()
{
int i, j, n, k, isprime = 1, prime2;
scanf("%d", &n);
for (i = 3; i <= n; i++)
{
int flag = 0; // changing point
for (j = 2; j <= i; j++)
{
if (i % j == 0)
break;
}
if (i == j)
{
prime2 = i + 6;
for (k = 2; k < prime2; k++) // changing point
{
if (prime2 % k == 0)
{
flag++;
break;
}
}
if (flag == 0)
{
printf("%d %d\n", i, prime2);
}
}
}
return 0;
}
Few resources to know more about finding out prime numbers:
Prime Numbers
C Program to Check Whether a Number is Prime or not
Sieve of Eratosthenes
You can use Sieve to speed up the program. It can generate all pairs in O(N log N) time. Here's the Algorithm.
Now, you have a boolean array, is_prime where is_prime[i] is true if i is a prime, false otherwise.
Now, iterate from i = 1 to i = N and check if is_prime[i] && is_prime[i + 6], if the condition is true, output the pair.

C - Print all prime numbers from 1 to 100 using arrays

OK so I got this challenge where I have to print all the primes from 1 to 100... However there is an error in my code that I am unable to find. Here is how I thought the problem should be done:
For any number from 3 to 100 check if there is any other number in the primes array that divides it. If there is the number is not prime. If there is not the number is prime and should be added to the array. Pretty simple, right ?
However it is not working.
Here is my code :
#include <stdio.h>
int main() {
int Primes[50] = {0};
int i, j, k;
Primes[0] = 2;
Primes[1] = 3;
for (i = 3; i < 101; i++) {
for (j = 2; j < 100; j++) {
if (i % Primes[j] != 0 && Primes[j] != 0) {
Primes[j] = i;
}
}
}
printf("Primes array : \n");
for (k = 0; k < 51; k++) {
printf("%d ", Primes[k]);
}
return 0;
}
instead of assuming the magic number as 25... that is k!=25, we can
replace it with i<=100.
#include <stdio.h>
#include <stdlib.h>
int main(void){
int Primes[50] = {0};
int i,j,k = 0;
Primes[0]=2;
Primes[1]=3;
for(i=0; i<=100; i++) {
for(j = 2; j<=i; j++) {
if(i % j == 0 ){
if(i == j)
Primes[k++]=i;
break;
}
}
}
printf("Primes array : \n");
for(int index = 0;index < k; index++) {
printf("%d\n", Primes[index]);
}
return 0;
}
When you do this:
if(i % Primes[j] != 0 && Primes[j] !=0)
{
Primes[j]=i;
}
You're saying "if the current number is not divisible by the given prime number, replace the given prime number with the current number". This is not what you want.
You need to check if the current number is not divisible by any prime number. So you need to loop though the list of primes to make sure your number isn't divisible by any of them, and if so add the number to the end of the list. You can do that as follows:
int num_primes = 0;
for (i=2;i<101;i++)
{
int is_prime = 1;
for(j=0; j<num_primes && is_prime; j++)
{
if(i % Primes[j] == 0)
{
is_prime = 0;
}
}
if (is_prime) {
Primes[num_primes++] = i;
}
}
In the above code, we use num_primes to count the number of primes we have so far, and is_prime to see if we found a prime that divides the current number. As you divide each number by a prime, if the remainder is 0 you know the number is not prime and set is_prime to 0. This also causes the inner loop to exit right away. Then if is_prime is still set at the end of the inner loop, you have a prime and you add it to the end of the list.
You also have an off-by-one error in the printing loop:
for(k=0;k<51;k++)
Since Primes has size 50, the largest valid index is 49. So change it to:
for(k=0;k<50;k++)
There are lot of issues in the algorithm you used. Use this simple version with issues addressed in the code.
int main(void){
int Primes[50] = {0};
int i,j,k = 0 /* use for prime count purpose */;
Primes[0]=2;
Primes[1]=3;
for(i=4 /* 3 is already stored */; k != 48; i++) { /* rotate loop until prime count doesn't reaches 48 */
for(j = 2; j<=i; j++) { /* i is the number you want to check whether its prime or not. SO rotate loop from 2 to "i" */
if(i % j == 0){ /* use i%j not i % Primes[j] as you want to check whether i is prime or not */
if(i == j) /* if its a prime numbur j reaches upto i */
Primes[k++]=i; /* store it */
break; /* comes out of inner loop */
}
}
}
printf("Primes array : \n");
for(int index = 0;index < k; index++) { /* rotate k times not some random 51 times */
printf("%d ", Primes[index]);
}
return 0;
}
The code above doesn't actually give what we want, but beautiful code nonetheless. Here is the edited code that gives the prime numbers from 1 to 100
int main(void){
int Primes[50] = {0};
int i,j,k = 0 /* use for prime count purpose */;
Primes[0]=2;
Primes[1]=3;
for(i=0 /* 3 is already stored */; k != 25; i++) { /* rotate loop until prime count doesn't reaches 48 */
for(j = 2; j<=i; j++) { /* i is the number you want to check whether its prime or not. SO rotate loop from 2 to "i" */
if(i % j == 0){ /* use i%j not i % Primes[j] as you want to check whether i is prime or not */
if(i == j) /* if its a prime numbur j reaches upto i */
Primes[k++]=i; /* store it */
break; /* comes out of inner loop */
}
}
}
printf("Primes array : \n");
for(int index = 0;index < k; index++) { /* rotate k times not some random 51 times */
printf("%d\n", Primes[index]);
}
return 0;
}

I want to make my prime number range finder program faster in C

im kinda new to programming ,and i have that exercise.I made a program that runs just right for small ranges of numbers,but for this exercise we are given a high range of nums,and it just takes much time to finish examining.
Any suggestions how can i make it faster?
#include <stdio.h>
#define START 190000000
#define END 200000000
int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
printf("EXMINING %d\r\n", i);
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{ c=1;
break;
}
}
if (c == 2) primenum = primenum + 1;
printf("Prime Numbers Found so far: %d\r\n", primenum);
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}
You can check if i is odd. If is even (except 2) is not a prime number.
Replace your inner loop with this,
for(j=2;j<=sqrt(i);j++)

This is a c program to find next greatest number with same digits. But not passing one test case

This is a C program to find the next greater number with the same digits. This program is working for all given test cases except one. When the input is 472, the expected output is 724. But my output is 247. Can anyone please help me to find the error?
logic I tried to solve this is :
Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is 534976, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is Not Possible.
Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For 534976, the right side of 4 contains 976. The smallest digit greater than 4 is 6.
Swap the above found two digits, we get 536974 in above example.
Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974. We get 536479 which is the next greater number for input 534976.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int N, dig[100], i = 0,j, temp, t, s, k, l, min, temp1;
scanf("%d", &N);
while (N > 0) {
dig[i] = N % 10;
i++;
N = N / 10;
}
for (j = 0; j <= i; j++) {
if (dig[j] > dig[j + 1]) {
s = j;
break;
}
}
min = dig[s];
//printf("%d ", min);
for (k = s; k >= 0; k--) {
if (dig[k] <= min) {
min = dig[k];
t = k;
}
}
//printf("%d ", t);
temp = dig[t];
dig[t] = dig[s + 1];
dig[s + 1] = temp;
for (k = 0; k <= s; k++) {
for (l = k + 1; l <= s; l++) {
if (dig[k] < dig[l]) {
temp1 = dig[k];
dig[k] = dig[l];
dig[l] = temp1;
}
}
}
for (k = i - 1; k >= 0; k--) {
printf("%d", dig[k]);
}
}
Your algorithm seems correct, but the loops are incorrect. Some index boundaries are off by one and the comparisons with <= are incorrect. Storing the digits by increasing powers of 10, while more practical is counter-intuitive and complicates the translation of the algorithm into code.
Here is a corrected version, that outputs all greater numbers. You can easily check the output by piping through sort -c to verify order and wc -l to verify that all combinations have been found (there should be at most n! - 1 greater numbers for a number with n digits).
#include <stdio.h>
int main() {
int N, dig[100], i, j, s, t, k, l, temp;
if (scanf("%d", &N) != 1 || N < 0)
return 1;
for (;;) {
for (i = j = 100; N > 0;) {
dig[--i] = N % 10;
N = N / 10;
}
for (s = j - 2; s >= i; s--) {
if (dig[s] < dig[s + 1]) {
break;
}
}
if (s < i) {
/* no greater number with the same digits */
break;
}
t = s + 1;
for (k = t + 1; k < j; k++) {
if (dig[k] < dig[t] && dig[k] > dig[s]) {
t = k;
}
}
temp = dig[t];
dig[t] = dig[s];
dig[s] = temp;
for (k = s + 1; k < j; k++) {
for (l = k + 1; l < j; l++) {
if (dig[k] > dig[l]) {
temp = dig[k];
dig[k] = dig[l];
dig[l] = temp;
}
}
}
N = 0;
for (k = i; k < j; k++) {
N = N * 10 + dig[k];
printf("%d", dig[k]);
}
printf("\n");
}
return 0;
}
Input: 472
Output:
724
742

find the prime numbers in C

I have this code for prime numbers..it gives me the prime numbers up to 103.Maybe my first break statement is wrong? I did it this way because i want to skip as much numbers as possible.i want only the primes that have at least two digits(that is why i started from 11)
#include <stdio.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j;
for (i = 11 ; i < MAXNUMB ; i += 2)
{
if ((i % 3 == 0) && (i % 5 == 0) && (i % 7 == 0))
break;
for (j = 3 ; j * j <= i ; j += 2)
{
if (i % j == 0)
break;
}
if (j * j > i)
printf ("%d \n", i);
}
}
Use continue rather then break; Here you want to skip much number ( As if there are many ways to find the prime numbers in small complexity like much popular Sieve of Eratosthenes) but it breaks. So change a little..
if((i%3==0)&& ( i%5==0) && (i%7==0))
continue;// here
it works..
#include <stdio.h>
#include <string.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j, P[MAXNUMB];
memset(P, 0, sizeof(P));
for(i = 2; i < MAXNUMB; ++i)
{
if(P[i] == 0){
printf("%d\n", i);
if(i < MAXNUMB / i)
for(j = i*i; j < MAXNUMB; j += i)
P[j] = 1;
}
}
return 0;
}
As it would be obvious using a debugger, that statement is wrong. First, you check for numbers that are divisible by 3 and 5 and 7. 105 is divisible by all. Then you break, which means "exit this for loop", so the program will end. You need to just continue the looping and not report this number as a prime.
You want to change the loop to use or instead of and. Also the next for loop doesn't need to start from 3 then, since you already tried 3, 5, 7. But is there a reason for that since you could just use the for loop?
My suggestion would be to just forget the first if altogether since it will not make it any faster.
#include <stdio.h>
#define MAXNUMB 100000000
int main (void)
{
int i, j, f;
for (i = 11; i < MAXNUMB; i += 2)
{
for (j = 2, f = 1; f && j * j < i; ++j)
{
f = (i % j != 0);
}
if (f) printf("%d\n", i);
}
return 0;
}

Resources