I'm trying to figure out a way to solve this problem. The user has to input a number N, and the program has to print the numbers that have GCD= 1 between them and N.
If the input is 5, then the program should print "1 2 3 4".
For input 6, then print should be "1 5".
Yet my code doesn't print anything. Could anyone help me out?
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b)
{
if (a==b)
return a;
else
if(a<b)
return gcd(a, b-a);
else
return gcd(a-b, b);
}
int main()
{
int i=0;
long int n;
scanf("%ld", &n);
if (n<1 || n>999999999)
printf("Wrong Input");
else
{
for(i=0; i<=n ; i++)
{
gcd(n, i);
if (gcd(n,i)==1)
printf("%d ", i);
}
}
return 0;
}
I changed the loop
for (int i=0;i<=n;i++) to (int i=1;i<=n;i++) because zero has infinity of divisors
int main()
{
int found=0;
long n;
scanf("%ld", &n);
if (n<1 || n>999999999)
printf("Wrong Input");
else
{
for(int i=1; i<=n ; i++)
{
gcd(i, n);
if (gcd(n,i)==1)
printf("%d ", i);
}
}
return 0;
}
int gcd(int a, int b)
{
if (a==b)
{
return a;
}
else if(a<b)
{
return gcd(a, b-a);
}
else
{
return gcd(a-b, b);
}
}
Try this:
int _gcd(int a, int b)
{
int c = a % b;
if (c == 0)
return b;
return _gcd(b, c);
}
int gcd(int a, int b)
{
return a > b ? _gcd(a, b) : _gcd(b, a);
}
In addition to that, you obviously need to change this:
for (i = 0; i <= n; i++)
To this:
for (i = 1; i < n; i++)
Print k-element subsets of an n element set (In essence n choose k)
represent each subset as an array and skip B[0].
For example [0 1 0 1] means {1,3} to print.
I believe my main problem is in my printSubsets() function because I am calling the method twice.
But it looks like it can't do that; it ignores the "2" in the index until the very end of the program.
Code
#include <stdio.h>
#include <stdlib.h>
void printSet(int B[], int n) {
for(int j = 1; j <= n; j++) {
printf("%d", B[j]);
}
}
void printSubsets(int B[], int n, int k, int i) {
if(i <= n) {
if(k == 0) {
printSet(B, n);
printf("\n");
}else{
B[i] = 1; //print 1 in the index of interest and recurse
printSubsets(B, n, k-1, i++);
B[i] = 2; //print 2 as a holder to ignore that index
printSubsets(B, n, k, i++);
}
}
}
int main(){
int n;
int k;
scanf("%d %d", &n, &k);
int B[101];
printSubsets(B, n, k, 1);
return EXIT_SUCCESS;
}
#include <limits.h>
#include <stdio.h>
#include <string.h>
// A utility function to find minimum of two numbers
int min(int a, int b)
{ return a < b ? a : b; }
int MinInsertions(char a[], int l, int h)
{
if (l > h) return INT_MAX;
if (l == h) return 0;
if (l == h - 1) return (a[l] == a[h])? 0 : 1;
// Check if the first and last characters are
// same. On the basis of the comparison result,
// decide which subrpoblem(s) to call
return (a[l] == a[h])?
MinInsertions(a, l + 1, h - 1):
(min(MinInsertions(a, l, h - 1),
MinInsertions(a, l + 1, h)) + 1);
}
int main()
{
int n;
char a[n];
int x=0;
scanf("%d",&n);
if (n>=3 && n<=5000)
{
scanf("%s",a);
printf("%d", MinInsertions(a, 0, strlen(a)-1));
}
else
{ printf("wrong input");
}
return 0;
}
PLease reply as soon as possible.
whats the problem with stadard input, in some compiler it is working.
Here is one problem.
You are using uninitialized variable to declare char array.
int n;
char a[n];
That will lead to undefined behavior. Instead declare the a after reading n.
I have got a piece of code that prints the combination of M number From N (nCm);
As it is a recursion, it works very slow when N is large.
#include <stdio.h>
#include <stdlib.h>
#define N 80
#define M 4
int result[M]= {0}; // THE ARRAY THAT SAVE THE RESULT OF ONE COMBINATION
int queue[N] = {0};
int top = 0;
void comb(int* input,int s, int n, int m)
{
if (s > n)
return ;
if (top == m)
{
for (int i = 0; i < m; i++)
{
result[i] = queue[i];
printf("%d\n", queue[i]);
}
}
queue[top++] = input[s];
comb(input,s+1, n, M);
top--;
comb(input,s+1, n, M);
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,
73,74,75,76,77,78,79,80};
printf("\ncombination():\n");
comb(array,0, N, M);
printf("\n");
}
I would like to know if there is any space for improvement in the algorithm above?
if possible, can I use openMP ?
Thanks
To me your code was even giving the desired output. see
I have changed
printing format each combination was not good enough.
repeated combinations. (note: else part of if statement added).
reduced 2 recursive call with a loop and a recursive call. (Less space.)
The required code is:
#include <stdio.h>
#include <stdlib.h>
#define N 20
#define M 6
int result[M]= {0}; // THE ARRAY THAT SAVE THE RESULT OF ONE COMBINATION
int queue[N] = {0};
int top = 0;
void comb(int* input,int s, int n, int m)
{
if (s > n)
return ;
if (top == m)
{
printf("\n");
for (int i = 0; i < m; i++)
{
result[i] = queue[i];
printf("%d ", queue[i]);
}
}else{
for(int ss=s;ss<n;ss++){
queue[top++] = input[ss];
comb(input,ss+1, n, m);
top--;
}
//comb(input,s+1, n, m);
}
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,
73,74,75,76,77,78,79,80};
printf("\ncombinations():\n");
comb(array,0, N, M);
printf("\n");
}
I'm trying to find nCr value. There is no error but I'm getting 1 as the answer for all the inputs. Help me find the solution please.
#include <stdio.h>
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*1;
}
return f;
}
int main(void)
{
int n,r,ncr=0;
printf("\n enter n and r values");
scanf("%d%d",&n,&r);
ncr=(fact(n) / (fact(r) * fact(n-r)));
printf("\n ncr for %d and %d is %d",n,r,ncr);
return 0;
}
It should not be f = f *1, but rather f = f * i
Your factorial code is incorrect.
You set f = 1, then do f = f * 1 a bunch of times. Then return f which is still 1. I think you mean f = f*i right?
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
Your method to compute factorial needs a correction. Should be :
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
Also, nCr is not defined if r > n. You should add this check after your scanf call.
Something like :
if (r > n) printf("r cannot be greater than n.").
A more standard form of computing factorial is one that uses recursion.
int fact(int num)
{
if (num == 1 || num == 0)
return 1;
else
return (num * fact(num - 1));
}
A better and faster approach would be to calculate nCr like this
int nCr(int n, int r) {
if (r > n / 2) r = n - r;
int ans = 1, i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}