I wrote this program in C for prime factors but its crashing after printing first prime factor, So i need help
#include<stdio.h>
void prime(int n,int i)
{
i=2;
if(n%i==0)
{
printf("%d ",i);
n=n/i;
prime(n,i);
}
else
{
i++;
prime(n,i);
}
}
void main()
{
int n;
scanf("%d",&n);
prime(n,2);
}
#include<stdio.h>
void prime(int n,int i)
{
if(n==0)
;
if(n==1)
;
else if(n%i==0){
printf("%d ", i);
n=n/i;
prime(n,i);
}
else{
i++;
prime(n,i);
}
}
int main()
{
int n;
scanf("%d",&n);
prime(n,2);
return 0;
}
If you add the cases for n==0 and n==1 (and remove the i=2;), you'll be all set.
edit - removed voided main as suggested by Jonathan Leffler
It's doing an infinite loop as you are resetting i every time when doing
i = 2;
But even without that, you'll probably overflow i and you don't have a recursion terminal case.
you will have to rethink your logic for this to work.
Related
The code given below passes two test cases and sieve of eratosthenes passes 1 test case. How can this problem be solved.
I have already tried miller rabin and sieve eratosthenes primality test.
None is passing all the test cases because of time restriction. Is there any possible way faster than these?
The below code is passing two of the 5 test cases. Can it be made any shorter in terms of time complexity?
#include<stdio.h>
#include<math.h>
int isPrime(int n)
{
int i;
int x=(int)(sqrt(n));
if(n==2)
return 1;
else if(n%2==0)
return 0;
else
{
for(i=3;i<=x;i+=2)
{
if(n%i==0)
{
return 0;
}
}
}
return 1;
}
int counting(int *a,int n)
{
int i,c=0;
for(i=0;i<n;i++)
{
if(isPrime(a[i]))
c++;
}
return c;
}
void main()
{
int cases,n,a[100000],i,j,count;
scanf("%d",&cases);
for(i=0;i<cases;i++)
{
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
count=counting(a,n);
printf("%d\n",count);
}
}
Maybe you should try this algorithm, i got from this site. It seems to be more time-efficient:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i)
{
// condition for nonprime number
if(n%i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
I have found the solution to the programs, but i got running time error. I need to simplify the sourse code, so it could be compiled in 0.3 second. Please help me simplify the program.
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, c=0, num;
scanf("%d", &num);
for(i=2; i<=num; i++)
{
for(j=1;j<=sqrt(i);j++)
{ if(i%j==0)
c++;
}
if(c==1)
{ printf(" %d", i);
}
c=0;
}
printf("\n");
return 0;
}
I am a beginner and I am learning functions. Following is the code I have written to find if the given number is a prime. But when ever I execute it, I get the dialogue box saying "project.exe has stopped working". I am using dev c++ . any problem with my code?
#include<stdio.h>
int prime (int i);
int main()
{
int a,b;
scanf("%d",&a);
b=prime(a);
if (b==1)
printf("prime");
else
printf("not prime");
return 0;
}
int prime (int i)
{
int j=0;
for (j=0;j<=i;j++)
{
if (i%j==0)
break;
}
if (j==i)
return 1;
else
return 0;
}
Change this code block of your prime function:
int j=0;
for (j=0;j<=i;j++)
{
if (i%j==0)
break;
}
to:
int j;
for (j=2;j<=i;j++)
{
if (i%j==0)
break;
}
When you are trying to find the mod it actually tries to find it by division operation. You cannot divide a number by zero, as j is initially zero, you program is crashing.
It should start from 2, not from 0. 0 causes division by zero and that's cause why your program stops. Correct is:
int j;
for (j=2;j<i;j++)
{
if (i%j==0)
break;
}
if (j==i)
return 1;
else
return 0;
// or simply return j == i;
You need to return a value that is not one. Something like this ...
int prime (int i) {
int j;
if (i <=3) return 1;
for (j=2;j<i/2;j++)
if (i%j==0) return j;
return 1;
}
Checking if the number i is greater than 3 is also important.
#include<stdio.h>
void countingSort(int array[], int k, int n){
int i,j;
int B[100],C[1000];
for (i=0;i<=k;i++)
{
C[i]=0;
}
for (j=0;j<n;j++)
{
C[array[j]]++;
}
for (i=1;i<=k;i++)
{
C[i]+=C[i-1];
}
for (j=0;j<n;j++)
{
B[--C[array[j]]]=array[j];
}
printf("Sortiran niz je: \n");
for(i=0;i<n;i++)
{
printf("%d ", B[i]);
}
printf("\n");
}
void max(int array[], int *k,int n){
int i;
printf("Broj elemenata u nizu je %d\n",n);
for(i=0;i<n;i++)
{
if(array[i]>*k) {
*k=array[i];
}
}
}
int main(int brArg, char *arg[]){
FILE *ulaz;
ulaz=fopen(arg[1],"r");
int array[1000];
int i=0,j,k=0,n,x,m;
while(fscanf(ulaz,"%d", &array[i])!=EOF)
i++;
fclose(ulaz);
n=i;
max(array,&k,n);
countingSort(array,k,n);
return 0;
}
My code works excellent for positive integers but I need to modify it so it can also sort negative integers. I hope you can help me. I don't have anything else to say but I can't post a question unless I write something here about it, so I hope it's enough.
Use a linked list, the generic list Glist exists in the GObject library. If you only need to deal with 32bit integers you can use it with very little extra programming. As you read the integers from file perfom an insertion sort in that while loop.
I was trying to print Pascal's Triangle using the following code. But after printing the first '1' the compiler runs into an error and I have to manually stop the execution. What might be the error in the code?
EDIT: fact function has been changed as shown with no difference whatsoever.
I'm using Codeblocks 10.05. A dialog window pops up and says that .exe has stopped working and Windows is searching for a solution.
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<2*row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
if(a==0)
printf("%d",1);
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num == 1)
{
return 1;
}
else return num*fact(num-1);
}
Your problem is that fact(0) will go into an infinite loop calling fact(-1), then fact(-2), and so on. Eventually it'll crash due to recursion running out of stack space.
How does fact(0) happen? when fact(a-b) is called when a == b.
This currently happens when row == 2, k == 1.
There are two mistakes that I can see
fact function: it's infinitely recursive
improper bounds of loop which calls comb function. There are row + 1 elements in each row, not 2 * row - 1. your formula works only for the first two rows.
Try this code
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n;
printf("Height: ");
scanf("%d",&n);
for(int row = 1; row <= n; row++)
{
if( row == 1)
printf(" ");
for(int j = 0;j < n - row; j++)
{
printf(" ");
}
for (int k = 0;k < row + 1;k++)
{
if ( row != 1)
comb(row,k);
else
{
printf("1");
break;
}
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
printf("%d ",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num > 1)
return num * fact(num-1);
else
return 1;
}
int main(){
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<=row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b){
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num){
if(num < 2)
return 1;
else
return num*fact(num-1);
}