BackTracking in C Sum of prime numbers? - c

I want to make a backtracking program to calculate the sum of every prime number smaller then n. Can you help me doing that ? I was working on a code but I do not know why it is not working ! Thx in advance !
I think I`m doing something wrong !
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int v[20],n;
void afisare(int k)
{
int i;
for(i=0;i<=k;i++)
{
printf("%d",v[i]);
}
}
int valid(int k)
{
int i,prim=0;
for(i=1;i<k;i++)
{
if(v[k]%i==0)
{
prim++;
}
}
if(prim==2)
{
return 1;
}
else
{
return 0;
}
}
void backtr(int k)
{
int val;
for(val=1;val<=n;val++)
{
v[k]=val;
if(valid(k))
{
if(k<n-1)
{
afisare(k);
}
else
{
backtr(k+1);
}
}
}
}
int main()
{
int n;
printf("n=");
scanf("%d",&n);
backtr(1);
return 0;
}

A bit late but I hope to help someone else
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int verify(int s) {
if (s == 0)
return 0;
else if (s == 1)
return 1;
else
int z = 0;
int i = 1;
while (i <= s) {
if (s % i == 0)
z++;
i++;
}
if (z == 2)
return s;
else
return 0;
}
int backback(int n, int s, int *sum) {
if (s == n) {
return;
}
int z = verify(s);
*sum = *sum + z;
backback(n, s + 1, sum);
return *sum;
}
int back(int n) {
if (n <= 0)
return 0;
else if (n == 1)
return 1;
else
int sum = 0;
int x = backback(n, 0, &sum);
return x;
}
int main(void) {
int n;
printf("Insert an integer number: \t");
scanf("%d", &n);
int x = back(n);
if (x == 0)
printf("\nInvalid number");
else if (x == 1)
printf("\nThe sum of prime numbers of the number %d is: \t%d", n, x);
else
printf("\nThe sum of prime numbers of the number %d is: \t%d", n, x);
return 0;
}

Related

Finding factorial with C

just a newbie in C here. I'm trying to print out the factorial given the input using recursion & pointers, but when my input is 5, the output was 2293620. Can somebody help me with this? I'm not sure where did that number come from, because factorial of 5 should give me 120. Thanks for your help!
#include<stdio.h>
int countlength(int *num) {
int x = 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
Remove references from your code.
#include<stdio.h>
int countlength(int num) {
int x = 1;
if (num == 1) {
return 1;
} else {
return num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
In this case, I think you don't need to use pass by reference. You should use pass by value. Then your code will work perfectlyly
Change this line:
return num * countlength(num - 1);
to this:
int val = *num - 1;
return *num * countlength(&val);
So you correctly assign the value and pass it as a pointer.
int countlength(int *num) {
int x = *num - 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(&x);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}```
**Try this**
int countlength(int *num) {
if (*num == 1) {
return 1;
} else {
return *num * countlength(&(*num - 1));
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
try this

why when I input any number between 14 and 20, the output is incorrect?

'I need to calculate and print an upside down pascal triangle, so I wrote down 2 functions for factorial and for the nCr, and I have followed, the equation x! / (y! * (x - y)!)'
#include <stdio.h>
#include <stdlib.h>
int Factorial (int value)
{
if (value == 1 || value == 0)
{
return 1;
}
return value*Factorial(value - 1);
}
int nCr(int value, int r)
{
return Factorial(value)/(Factorial(r) * Factorial(value - r));
}
int main(int argc, char **argv)
{
int value, i, j, k;
char* p;
value = strtol(argv[1], &p, 10);
if (*p != '\0')
{
return 1;
}
if (argc != 2)
{
return 1;
}
if (value < 1 || value > 20)
{
printf("Error: Please enter a value between 1 and 20 inclusively\n");
return 1;
}
else
'The problem is supposed to be in the nested loops I guess'
{
for (i = value - 1; i >= 0; i--)
{
for (j = value - i; j > 0; j--)
{
printf(" ");
}
for (k = 0; k <= i; k++)
{
printf("%d ", nCr(i, k));
}
printf("\n");
}
}
return 0;
}
The problem was in the declaration of the Factorial functions, as int type would overflow if the input is more than 13!, so we should declare the Factorial function as long, so that it does not overflow.

I can not determine if it is a prime number

The below program correctly outputs the divisors of the input numbers, but it does not correctly report whether the inputs are prime. For example, when the input is 13, it does not print "The number you entered is a prime number." What's wrong with it?
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
printf("Enter a number: ");
while (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.");
}
return 0;
}
the reason is that scanf is in a while loop if there's a valid input but you are checking & printing if it's prime outside of the loop... if you expect this program just get one input and validate it once, then you just need to change that while to if:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
printf("Enter a number: ");
if (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.");
}
return 0;
}
If you expect this program goes in a loop to keep on getting input and validating if it's prime or not, this should do the job:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
while (1)
{
isPrime=true;
printf("Enter a number: ");
if (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.\n");
}
}
return 0;
}
You have missed 2 things !
You should have printed inside the while loop !
In addition to that you didn't change the status of isPrime=true; !
Hope this answers your question !
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
while (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
if (isPrime)
{
printf("The number you entered is a prime number.\n");
}
isPrime=true;
}
return 0;
}
When you entered a prime number, your while loop doesn't break. Try it:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int num;
bool isPrime = true, finishIt = false;
printf("Enter a number: ");
while (1) {
while (1) {
if (scanf("%d", &num) != 1)
continue;
if (num == 0) {
finishIt = true;
break;
}
int i;
for (i = 2; i * i <= num; ++i) {
if (num % i == 0) {
if (i * i != num) {
printf("%d ve %d, divides %d\n", i, num / i, num);
} else {
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
if (i * i >= num)
break;
}
if (isPrime) {
printf("The number you entered is a prime number.");
}
isPrime = true;
if (finishIt)
break;
}
return 0;
}

Is this code correct for checking for prime number in c using functions?

Is this code correct for checking prime number using functions. I am not getting any syntax errors but it is always showing prime regardless of what I enter.
#include<stdio.h>
int prime(int);
void main() {
int n, count, a;
printf("enter the number\n");
scanf("%d", &n);
prime(n);
if (count == 2)
printf("prime");
else
printf("not prime");
}
int prime(int n) {
int i, count = 0;
for (i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
return (count);
}
}
Modify your function prime -
int prime(int n) {
int i, count = 0;
for (i = 1; i <n; i++) { // go till n (or better till sqrt of n)
if (n % i == 0) {
count++;
}
if(count==2){ // if count is 2 get out of loop
break;
}
}
return count; // return out of loop
}
In you function main-
1. void main -> int main(void)
2. write you if else satatements as follows -
if (count == 2)
printf("not prime");
else
printf("prime");
3. initialize count to 0 in main.
call function as -
count=prime(n);
Your prime function doesn't work properly. In the first for loop you're doing n & 1 == 0 which always is true, and in the last loop n % n == 0 also is always true
int prime(int n){
int i;
int isPrime = 1;
for(i=2;i<sqrt(n) && isPrime;i++){ // do the loop while 2<i<sqrt(n) and n isPrime, once you know it's not prime don't loop anymore
isPrime = n%i; // If it's 0 then it's not prime (isPrime = 0) else it's true
}
return isPrime;
}
Edit: Optimized the for loop until i=sqrt(n)
/* i just needed to take input of value count and store it in a variable*/
#include<stdio.h>
int prime(int);
int main(void)
{
int n,count,a,m;
printf("enter the number\n");
scanf("%d",&n);
m=prime(n);
if(m>2)
printf("not prime");
else
printf("prime");
}
int prime(int n)
{
int i,count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
return(count);
}
A simple function return true if the number is prime and false if not.
int prime(int n){
int i;
for(i= 2; i < n; i++){
if(n % i ==0 && i != n)
return true;
}
return false;
}

Why does my program return the same character?

This is a program that checks whether the input number is a product of two prime numbers ('Y') or not ('N').
#include <stdio.h>
// the function checks if the number is prime
int is_prime(int z) {
int i;
for(i=2; i<z; i++){
if(z%i == 0){
return 0;
}
return 1;
}
}
/* the function checks if the given number is a
product of two prime numbers bigger than 2*/
int is_prime_product(int x) {
int j;
for(j=2; j<x; j++){
if(x%j == 0){
if(is_prime(x/j) && is_prime(j)){
return 1;
break;
}else return 0;
}
}
}
int main() {
int n=0;
int c;
do{
c=getchar();
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
return 0;
}
I don't know why this program always returns 'Y' even when it should return 'N'. I just don't see where the mistake is.
Partial answer: Better version of is_prime:
int is_prime(int z)
{
if(z <= 1) return 0;
if(z == 2) return 1;
int i;
for(i=3; i<sqrt(z); i+=2)
{
if(z%i == 0) return 0;
}
return 1;
}
After the test for 2 it is enough to test for odd factors up to the square root of your test number. (Also fixed the curly braces)
Cause of your error:
if(is_prime_product(n)) ...
tests the input number nnot the last character c
Edit
Some hints for better (more readable, more reliable, and so on) code:
Use types matching the problem (bool instead of int)
Use good variable names (i only for loops, z only for floats)
use meaningful variable names (number instead of n)
consistent braces, spacing around operators
These things make a difference!
Have a look:
bool is_prime(unsigned int number)
{
if(number <= 1) return false;
if(number == 2) return true;
for(unsigned int factor = 3; factor < sqrt(number); factor += 2)
{
if(number % factor == 0) return false;
}
return true;
}
Please cheak your is_prime() function. The code return 1; should be after the for loop.You can try the following:
int is_prime (int z)
{
int i;
for (i = 2; i < z; i++)
{
if (z % i == 0)
{
return 0;
}
}
return 1;
}
and your function is_prime_product () should be written as follow:
int is_prime_product (int x)
{
int j;
for (j = 2; j < x; j++)
{
if (x%j==0&&is_prime (x / j) && is_prime (j))
{
return 1;
}
}
return 0;
}
also you should use if (is_prime_product (n)) instead of if (is_prime_product (c)).
I have modified some of your code from main function.
Please try with below code once,
Instead of this main function code:
int main() {
int n=0;
int c;
do{
c=getchar();
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
return 0;
}
Use this code:
int main() {
int n=0;
int c;
scanf("%d",&c);
do{
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
return 0;
}

Resources