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;
}
Related
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.
So here is the problem: Write a program that accept an integer n, print out the largest number but smaller or equal n that is the product of two consecutive even number. Example: Input: 12, Output: 8 ( 2x4 )
Here is my code :
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = n; i >= 0; i--)
{
for (int j = 0; j <= n; j = j + 2)
{
if ( i == j * (j+2) )
{
printf("%d ", i);
break;
}
}
}
return 0;
}
So if i input 20, it will print out 8 and 0 instead of 8, if i input 30, it will print out 24,8 and 0 instead of just 24. How do i make it stop after printing out the first number that appropriate ?
You need to stop an outer loop from processing, for example by using a boolean flag (meaning "solution found, we finish work") or a goto statement.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int solutionFound = 0;
for (int i = n; i >= 0; i--) {
// this could also be put into for's condition i.e. "i >= 0 && !solutionFound"
if (solutionFound) {
break;
}
for (int j = 0; j <= n; j = j + 2) {
if ( i == j * (j+2) ) {
printf("%d ", i);
solutionFound = 1;
break;
}
}
}
return 0;
}
EDIT: immediate return as noted in the comments is also a nice idea, if you don't need to do anything later.
Your problem is that you are nested - in a for loop which is inside another for loop - when you want to stop processing.
Some languages would let you code break 2; to indicate that you want to break out of 2 loops. Alas, C i snot such a language.
I would recommend that you code a function. That would serve a few porpoises: 1) your main should be "lean & mean" 2) as your programs get larger, you will learn the benefits of putting individual coding tasks into functions 3) you can use return; instead of break; and it will exit the function immediately.
Something like this:
#include <stdio.h>
void FindNeighbouringDivisors(int n)
{
for (int i = n; i >= 0; i--)
{
for (int j = 0; j <= n; j = j + 2)
{
if ( i == j * (j+2) )
{
printf("%d times %d = %d", j, j + 2, i);
return;
}
}
}
printf("There are no two adjacent even numbers which can be multiplied to give %d", n);
}
int main()
{
int n;
scanf("%d", &n); /* could get from comamnd line */
FindNeighbouringDivisors(n);
return 0; /* should be EXIT_SUCCESS */
}
Btw, when you have a problem with your code, ask a question here. When you have it working, consider posting it at our code review site where more experienced programmers can give you advice on how to improve it. It's a great way to learn
Break only breaks you out of immediate loop, so either use flags or just use return to terminate the execution. Or you can even use following code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int j = 0; j <= n; j = j + 2)
{
if ( n < j * (j+2) )
{
printf("%d ", j*(j-2));
break;
}
}
return 0;
}
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);
}
I have following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, br, j=1, zbroj=0;
printf("Ucitati cijeli broj(manji od 1000):\n");
scanf("%d", &n);
for(br=1; br<=n; br++)
{
if(br % 6 == 0)
printf("%d ", br);
j = br;
while(j != 0)
{
if(j % 6 == 0)
zbroj++;
j /= 6;
}
}
printf("\nPerica je zapisao %d cifara\n", zbroj);
return 0;
}
As I was doing some practice in C, I encountered on unusual problem.So my count(zbroj) prints how many numbers were printed(in this case 3). But it won't print how many digits it has(if I input 18, it should print total digits (6,12,18), and that is 5 digits)). So I'm little confused why it prints total of numbers, but not total of digits.
The only place the value if zbroj is modified is in the loop:
j = br;
while(j != 0)
{
if(j % 6 == 0)
zbroj++;
j /= 6;
}
This is not going to count the number of digits in each number. It's going to count the number of times each number is divisible by 6 into zbroj.
To count digits, you can replace your loop with:
if (br % 6 == 0)
{
printf("%d ", br);
j = br;
while (j != 0)
{
zbroj++;
j /= 10;
}
}
Also notice that I moved the while loop into the scope of the if statement so that only the numbers that are actually printed out are counted by zbroj.
For brevity, you could also #include <math.h> and use (int) log10(j) + 1 to count digits on a match.
I'm writing a program to find all of the prime numbers contained within a user input n. I am having trouble with the is_prime function.
#include <stdio.h>
#include <math.h>
main() {
int n;
int k;
// gets user input for length of string
// and stores it as n
printf("Enter the value of n:");
scanf("%d", &n);
for (k = 2; k <= n; k++) {
if (is_Prime(k) == 1) {
printf("Printing primes less than or equal to %d: /n %d, &n, &k");
}
}
I want the output to look like this, but I am not sure how to print the list without using different variables for each prime number.
Printing primes less than or equal to 30:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29.
//here is the is_Prime function
is_Prime (int n)
{
for(j = 2; j <= n/2; j++)
{
if(n%j != 0)
{
return 1;
break;
}
}
if(n%j == 0 )
return 0;
}
I am not sure how to call the is_prime subroutine? Any help?
printf("Printing primes less than or equal to %d:\n", n);
for(k = 2; k <= n; k++)
{
if(is_Prime(k) == 1)
{
printf("%d, ", k);
}
}
printf("Printing primes less than or equal to %d:\n%s", n, (n >= 2 ? "2" : ""));
for (k = 3; k <= n; ++k)
if (is_Prime(k))
printf(", %d", k);
printf("%s\n", (n >= 2 ? "." : ""));
Here's a slightly cleaner version of your is_Prime function:
int is_Prime(int n)
{
if (n < 2)
return 0;
int last = (int) sqrt(n) + 1; /* conservatively safe */
for (int j = 2; j <= last; ++j)
if (0 == n % j)
return 0;
return 1;
}
Note that you only really need to check up to the sqrt() of a number to find all its potential factors.
Also note that this is not a great way to find all the primes less than n, which is the prime purpose of your program, especially when you will repeatedly call this function incrementing n by 1 each time. I recommend trying to implement the Sieve of Eratosthenes or the Sieve of Sundaram instead -- so long as n isn't too large.