Program that prints the 2 largest prime numbers in a given range from the user using recursion.
I want to get the 2 largest prime numbers in a range that is given by the user, but somehow it just prints the consecutive numbers in the range. The prime checker in my code is working when I built a program that asks the user for the end number, checks if it is a prime number and prints all prime numbers from 1 to n. But when I plugged it in this code, its not working well. I tried researching for other codes to be my reference but I cant find anything that prints the 2 largest prime numbers. It doesn't matter if the users input are prime numbers or not, the code will just check the two largest prime numbers in between. The end number or y can also be printed if its a prime number. I'm also trying to not use arrays to practice my parameter passing.
In this code, I'm trying to get the 2 largest prime number and print it. You can see the num1, and num2 are unused, they are supposed to be the variables for the 2 largest prime numbers in the given range.
#include <stdio.h>
void usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);
int main() {
int x, y, i, j, num1, num2;
usersInput(&x, &y);
if (x > y) {
swapping(&x, &y);
}
printf("The value of x is %d, and the value of y is %d.\n", x, y);
printf("\nThe two largest prime numbers from %d to %d are : ", x, y);
for (i = x; i <= y; i++)
if (primeCheck(i, y) == 0)
printf("%d ", i);
return 0;
}
void usersInput(int *x, int *y) {
printf("Enter the value of x: ");
scanf("%d", x);
printf("Enter the value of y: ");
scanf("%d", y);
}
void swapping(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int primeCheck(int i, int j) {
if (j == i) {
return 0;
} else if (j % i == 0) {
return 1;
} else {
return primeCheck(i + 1, j);
}
}
As you can see, I don't have any functions yet for getting the 2 largest prime numbers. I don't really have idea on how to, so please help me
Here's the actual output:
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 6 7 8 9 10
Here's the expected output
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7
The sub-statement of this if statement that is the call of printf:
if (primeCheck(i,y)==1)
printf("%d ", i);
gets the control when the function primeCheck returns 1.
But according to the function definition it returns 1 when y is divisible by any number in the range [i, y] and it is not necessary that this number is i:
} else if (j%i==0) {
return 1;
In all other case the function returns 0.
As the function calls itself recursively then it returns 1 when at least for one number in the range [x, y] y is divisible by that number.
For example for your input the function always will return 1 for each number if the range [3, 5] because 10 is divisible by 5.
Actually your function does not make a sense and has nothing common with prime numbers.
Also pay attention to that to find two biggest prime numbers in a range you should start the loop from the biggest number down to the smallest number of the range.
I think you understand the assignment incorrectly. It seems you need to write non-recursive function that determines whether a give number is a prime number and write a recursive function instead of the for loop that returns two greatest prime numbers.
That is the function that recursively finds two greatest prime numbers can look something like shown in the demonstration program below:
#include <stdio.h>
struct TwoPrimes
{
unsigned int first_prime;
unsigned int second_prime;
};
int is_prime( unsigned int n );
struct TwoPrimes find_two_primes( unsigned int first, unsigned int last )
{
if (first == last)
{
struct TwoPrimes two_primes = { ( is_prime( first ) ? last : 0 ), 0 };
return two_primes;
}
else
{
struct TwoPrimes two_primes = find_two_primes( first + 1, last );
if (two_primes.first_prime == 0 || two_primes.second_prime == 0)
{
if (is_prime( first ))
{
if (two_primes.first_prime == 0)
{
two_primes.first_prime = first;
}
else
{
two_primes.second_prime = first;
}
}
}
return two_primes;
}
}
int main( void )
{
struct TwoPrimes two_primes = find_two_primes( 3, 10 );
if (two_primes.first_prime != 0)
{
printf( "%u\n", two_primes.first_prime );
if (two_primes.second_prime != 0)
{
printf( "%u\n", two_primes.second_prime );
}
}
}
What you need to do yourself is to define the function is_prime.
There are multiple problems in your code:
the test for primality if (primeCheck(i, y) == 0) is incorrect: you should pass 2 as the first argument and i as the second.
the loop should start at i = y and run while i >= x, decrementing i to find the largest primes in the range first.
you should store the primes and stop when 2 have been found.
the primality test function should be modified to return 1 if i > j to prevent infinite recursion on some inputs, such as primeCheck(2, 1).
testing divisors up to and including sqrt(j) is sufficient in primeCheck(). You can stop the recursion when j < i * i, which can be tested without potential overflow as j / i < i.
Here is a modified version of your code:
#include <stdio.h>
int usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);
int main() {
int x, y, i, num1, num2;
if (!usersInput(&x, &y)) {
return 1;
}
if (x > y) {
swapping(&x, &y);
}
printf("The value of x is %d, and the value of y is %d.\n", x, y);
for (i = y, num1 = num2 = 0; i >= x; i--) {
if (primeCheck(2, i) == 0) {
if (num2 == 0) {
num2 = i;
} else {
num1 = i;
break;
}
}
}
printf("The two largest prime numbers from %d to %d are: %.0d %.0d\n",
x, y, num1, num2);
return 0;
}
// read user input return zero on failure
int usersInput(int *x, int *y) {
printf("Enter the value of x: ");
if (scanf("%d", x) != 1)
return 0;
printf("Enter the value of y: ");
return scanf("%d", y) == 1;
}
void swapping(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int primeCheck(int i, int j) {
if (j < 2) {
return 1; // negative, 0 or 1: not prime
} else if (j / i < i) {
return 0; // all factors tested up to sqrt(j): j is prime
} else if (j % i == 0) {
return 1; // composite, not prime
} else {
return primeCheck(i + 1, j);
}
}
Output:
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7
Related
#include<stdio.h>
int factorial(int a){
int b;
if (a==0)
return 1;
else
for (b=1;b<=a;b++)
a=a*b;
} //factorial of a
int pascal(int i, int j){
return (factorial(i))/((factorial(j))*factorial(i-j));
}
int main()
{
int k,n,m,q;
printf("Input m: ");
scanf("%d", &m);
for(k=0;k<m;k++){
for(n=0;n<=k;n++)
printf("%d ", pascal(k, n));
printf("\n");
}
}
I make a program that print the Pascal triangle but it make the right answer if height<=3, when I input m=6; it will print:
1
1 1
1 3 1
1 0 0 1
1 0 0 0 1
1 0 0 0 0 1
Can you help me find the bugs in my code?
that's because you have written the wrong logic in the function factorial().
for (b=1;b<=a;b++)
a=a*b;//also , you have to return the value of a here!
here, suppose a = 3, then, in 1st iteration, a = 3*1 equals 3, in 2nd iteration, a=3*2 equals 6; since 6>3 ,loop terminates and you got the right answer!!
but if a = 4, then, in 1st iteration, a = 4*1 equals 4, in 2nd iteration, a=4*2 equals 8 which is greater than 4, therefore loop does not executes further and you got the wrong answer!
the correct logic would be, to declare another int variable and initialize it to 1 to store the product of 'a' and the variable in it itself i.e,
int fac=1;
for(b=1;b<=a;b++){
fac = fac*b;
}
return fac;
so, the full code will be,
#include<stdio.h>
int factorial(int a){
int b=0;
if (a==0)
return 1;
else{
/* for(b=1;b<=a;b++){
fac = b*(factorial(b-1));// another way to find factorial using recursion
return fac;
}*/
int fac=1;
for(b=1;b<=a;b++){
fac = fac*b;
}
return fac;
}
} //factorial of a
int pascal(int i, int j){
return factorial(i) / (factorial(j) *factorial(i-j));
}
int main()
{
int k,n,m,q;
printf("Input m: ");
scanf("%d", &m);
for(k=0;k<m;k++){
for(n=0;n<=k;n++)
printf("%d ", pascal(k, n));
printf("\n");
}
return 0; // a good habit to add return 0, to let the compiler know that
// the code is executed completely
}
I'm having some problems with this C programming assignment that I have in school. I'm supposed to return the prime numbers from within a given range, and it has to be done using recursion.
The code I've got so far is this:
#include <stdio.h>
#include <stdlib.h>
int primeNumberList(int n, int m, int z);
int main() {
int n1 = 0,
n2 = 10,
d = 2;
printf("n1 = %d | n2 = %d | d = %d\n\n", n1, n2, d);
printf("Prime Numbers between %d and %d are: \n", n1, n2);
primeNumberList(n1, n2, d);
printf("\n\n");
return 0;
}
int primeNumberList(int n, int m, int z) {
int notPrime = 0;
if (n <= 1) {
primeNumberList(n + 1, m, z);
} else
if (n < m) {
if (z <= n / 2) {
if (n % z == 0) {
notPrime = 1;
z = 2;
} else {
primeNumberList(n, m, z + 1);
}
}
if (notPrime == 0) {
printf("%d ", n);
}
primeNumberList(n + 1, m, z);
}
}
What happens when I run this, is that after it's gone through all the numbers up to the limit (in the function it's m (n2 in main)) it won't break the recursion, but somehow manage to subtract numbers from n, and starts printing some other numbers that are not prime numbers.
When I run it in debug, it seems to be looping at the end, but there's nothing there to loop... I've tried adding a return 0; or even a printf with some text, but it ignores it completely.
Can anyone see what I've done wrong here? Why doesn't it stop when n < m?
I found your problem. You have the potential to make two recursive calls for each time you call primeNumberList.
After you return from primeNumberList(n, m, z+1); (under the innermost else) you still can go on to print a prime and do a call to primeNumberList(n+1, m, z);. This is not the behavior you want, you want to return directly after this inner else call.
So simply add a return before each of your calls to primeNumberList (primeNumberList(x); becomes return primeNumberList(x);) and also a return 0 at the end of this function (this last return is just to make the compiler happy).
Try something cleaner: defining a separate isPrime function and calling it.
http://www.cquestions.com/2011/08/prime-number-program-in-c-using.html
int isPrime(int num,int i){
if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}
Your recursive function exit criteria is not right.
When you you call the recursive function, it winds up, then down. When I run your code through step by step, As it is winding up, I get the complete list of primes, as following:
Then as it continues and the following digits, are printed:
Your question: Why doesn't it stop when n < m?
Because as you start unwinding, the values stored in the value n also start unwinding down through the iterations of the recursions that have been called, allowing execution flow to stay in the loop.
Further more, if unwind brings execution flow to a point that passes the printf("%d ", n); statement, the value for n, whatever it is at that unwind iteration, will be printed out.
One way way to leave without printing anything beyond n == 10 is to create a bypass variable, and use it as a criteria for printing:
static done = 0;
Then set done to 1 when you do not want to print any more values as it is unwinding.
Here is a modified function that will do this:
int primeNumberList(int n, int m, int z) {
int notPrime = 0;
static done = 0;//add a bypass variable, init to zero
if (n <= 1) {
primeNumberList(n + 1, m, z);
}
else
{
if(n == 10)
{
done = 1; //at this point all primes (except 1) are printed
//so set done to 1
}
if (n < m)
{
if (z <= n / 2)
{
if (n % z == 0)
{
notPrime = 1;
z = 2;
}
else
{
primeNumberList(n, m, z + 1);
}
}
if ((notPrime == 0) && (!done)) //test done before printing
{
printf("%d ", n);
}
primeNumberList(n + 1, m, z);
}
}
return 0;//add this return statement
}
I need to write a C program which will read a number (in base 10) from user input and output it in any base which is a power of 2. The calculations have to be performed in one function, to_base_n, which takes the parameters num and base and prints the number in the respective base. As a validation check, the program also checks if the base is a power of two with the isPowerofTwo function.
The way the conversion is carried out is by means of long division which carries out the logic in the pseudocode below:
void to_base_n(int x, int n){
int r, i = 0
int digits[16]
while (x ≠ 0){
r = x mod n
x = x / n
digits[i] = r
i++
}
for (i = 0, i < 15, i++)
print digits[i]
}
Which I believe is arithmetically sound. But when I try to, for example, convert 82000 to base 4, I get the following output:
The large digits appearing are even bigger than num itself, so I figured the modulus cannot be entering the array properly (because ∀{x,n}; x mod n < x). I can't seem to find what's wrong with it. The full code is listed below.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
bool isPowerofTwo(int);
void to_base_n(int, int);
int main(){
//Variables
int num, base;
//Prompt
printf("Please enter a number in base 10: ");
scanf("%d", &num);
printf("Please enter a base (2^n) to convert it to: ");
scanf("%d", &base);
//Precaution
while(!isPowerofTwo(base)){
printf("That number is not a power of 2. Please try again: ");;
scanf("%d", &base);
}
if(isPowerofTwo(base)){
//Output
printf("The number %d (base 10) is equivalent to ", num);
to_base_n(num, base);
printf(" (base %d).", base);
}
//Return Statement
return 0;
}
//Checks if Base is a Power of Two
bool isPowerofTwo(int base){
while((base % 2 == 0) && base > 1){
base = base / 2;
if(base == 1){
return true;
break;
}
}
return false;
}
//to_base_n
void to_base_n(int x, int n){
int r, i = 0;
int digits[16];
while(x != 0){
r = x % n;
x = x / n;
digits[i] = r;
i++;
}
for(i = 0; i < 15; i++)
printf("%d|",digits[i]);
}
Can anyone help explain what's wrong with it?
The number 82000 in base 4 would be:
110001100
Which is exacly what you get. Your mistake is that:
They are printed backwards.
You are printing more digits than you should, so you print garbage.
You ignore the number of digits extracted with your pseudo code, so you print uninitialised elements of the array.
for (i = 0, i < 15, i++)
print digits[i]
And they are printed in reverse order. I suggest changing it to this
for (i = i - 1, i >= 0, i--)
print digits[i]
and as C code in your function
for(i = i - 1; i >= 0; i--)
printf("%d|",digits[i]);
I have a problem, then given some input number n, we have to check whether the no is factorial of some other no or not.
INPUT 24, OUTPUT true
INPUT 25, OUTPUT false
I have written the following program for it:-
int factorial(int num1)
{
if(num1 > 1)
{
return num1* factorial(num1-1) ;
}
else
{
return 1 ;
}
}
int is_factorial(int num2)
{
int fact = 0 ;
int i = 0 ;
while(fact < num2)
{
fact = factorial(i) ;
i++ ;
}
if(fact == num2)
{
return 0 ;
}
else
{
return -1;
}
}
Both these functions, seem to work correctly.
When we supply them for large inputs repeatedly, then the is_factorial will be repeatedly calling factorial which will be really a waste of time.
I have also tried maintaining a table for factorials
So, my question, is there some more efficient way to check whether a number is factorial or not?
It is wasteful calculating factorials continuously like that since you're duplicating the work done in x! when you do (x+1)!, (x+2)! and so on.
One approach is to maintain a list of factorials within a given range (such as all 64-bit unsigned factorials) and just compare it with that. Given how fast factorials increase in value, that list won't be very big. In fact, here's a C meta-program that actually generates the function for you:
#include <stdio.h>
int main (void) {
unsigned long long last = 1ULL, current = 2ULL, mult = 2ULL;
size_t szOut;
puts ("int isFactorial (unsigned long long num) {");
puts (" static const unsigned long long arr[] = {");
szOut = printf (" %lluULL,", last);
while (current / mult == last) {
if (szOut > 50)
szOut = printf ("\n ") - 1;
szOut += printf (" %lluULL,", current);
last = current;
current *= ++mult;
}
puts ("\n };");
puts (" static const size_t len = sizeof (arr) / sizeof (*arr);");
puts (" for (size_t idx = 0; idx < len; idx++)");
puts (" if (arr[idx] == num)");
puts (" return 1;");
puts (" return 0;");
puts ("}");
return 0;
}
When you run that, you get the function:
int isFactorial (unsigned long long num) {
static const unsigned long long arr[] = {
1ULL, 2ULL, 6ULL, 24ULL, 120ULL, 720ULL, 5040ULL,
40320ULL, 362880ULL, 3628800ULL, 39916800ULL,
479001600ULL, 6227020800ULL, 87178291200ULL,
1307674368000ULL, 20922789888000ULL, 355687428096000ULL,
6402373705728000ULL, 121645100408832000ULL,
2432902008176640000ULL,
};
static const size_t len = sizeof (arr) / sizeof (*arr);
for (size_t idx = 0; idx < len; idx++)
if (arr[idx] == num)
return 1;
return 0;
}
which is quite short and efficient, even for the 64-bit factorials.
If you're after a purely programmatic method (with no lookup tables), you can use the property that a factorial number is:
1 x 2 x 3 x 4 x ... x (n-1) x n
for some value of n.
Hence you can simply start dividing your test number by 2, then 3 then 4 and so on. One of two things will happen.
First, you may get a non-integral result in which case it wasn't a factorial.
Second, you may end up with 1 from the division, in which case it was a factorial.
Assuming your divisions are integral, the following code would be a good starting point:
int isFactorial (unsigned long long num) {
unsigned long long currDiv = 2ULL;
while (num != 1ULL) {
if ((num % currDiv) != 0)
return 0;
num /= currDiv;
currDiv++;
}
return 1;
}
However, for efficiency, the best option is probably the first one. Move the cost of calculation to the build phase rather than at runtime. This is a standard trick in cases where the cost of calculation is significant compared to a table lookup.
You could even make it even mode efficient by using a binary search of the lookup table but that's possibly not necessary given there are only twenty elements in it.
If the number is a factorial, then its factors are 1..n for some n.
Assuming n is an integer variable, we can do the following :
int findFactNum(int test){
for(int i=1, int sum=1; sum <= test; i++){
sum *= i; //Increment factorial number
if(sum == test)
return i; //Factorial of i
}
return 0; // factorial not found
}
now pass the number 24 to this function block and it should work. This function returns the number whose factorial you just passed.
You can speed up at least half of the cases by making a simple check if the number is odd or even (use %2). No odd number (barring 1) can be the factorial of any other number
#include<stdio.h>
main()
{
float i,a;
scanf("%f",&a);
for(i=2;a>1;i++)
a/=i;
if(a==1)
printf("it is a factorial");
else
printf("not a factorial");
}
You can create an array which contains factorial list:
like in the code below I created an array containing factorials up to 20.
now you just have to input the number and check whether it is there in the array or not..
#include <stdio.h>
int main()
{
int b[19];
int i, j = 0;
int k, l;
/*writing factorials*/
for (i = 0; i <= 19; i++) {
k = i + 1;
b[i] = factorial(k);
}
printf("enter a number\n");
scanf("%d", &l);
for (j = 0; j <= 19; j++) {
if (l == b[j]) {
printf("given number is a factorial of %d\n", j + 1);
}
if (j == 19 && l != b[j]) {
printf("given number is not a factorial number\n");
}
}
}
int factorial(int a)
{
int i;
int facto = 1;
for (i = 1; i <= a; i++) {
facto = facto * i;
}
return facto;
}
public long generateFactorial(int num){
if(num==0 || num==1){
return 1;
} else{
return num*generateFactorial(num-1);
}
}
public int getOriginalNum(long num){
List<Integer> factors=new LinkedList<>(); //This is list of all factors of num
List<Integer> factors2=new LinkedList<>(); //List of all Factorial factors for eg: (1,2,3,4,5) for 120 (=5!)
int origin=1; //number representing the root of Factorial value ( for eg origin=5 if num=120)
for(int i=1;i<=num;i++){
if(num%i==0){
factors.add(i); //it will add all factors of num including 1 and num
}
}
/*
* amoong "factors" we need to find "Factorial factors for eg: (1,2,3,4,5) for 120"
* for that create new list factors2
* */
for (int i=1;i<factors.size();i++) {
if((factors.get(i))-(factors.get(i-1))==1){
/*
* 120 = 5! =5*4*3*2*1*1 (1!=1 and 0!=1 ..hence 2 times 1)
* 720 = 6! =6*5*4*3*2*1*1
* 5040 = 7! = 7*6*5*4*3*2*1*1
* 3628800 = 10! =10*9*8*7*6*5*4*3*2*1*1
* ... and so on
*
* in all cases any 2 succeding factors inf list having diff=1
* for eg: for 5 : (5-4=1)(4-3=1)(3-2=1)(2-1=1)(1-0=1) Hence difference=1 in each case
* */
factors2.add(i); //in such case add factors from 1st list " factors " to " factors2"
} else break;
//else if(this diff>1) it is not factorial number hence break
//Now last element in the list is largest num and ROOT of Factorial
}
for(Integer integer:factors2){
System.out.print(" "+integer);
}
System.out.println();
if(generateFactorial(factors2.get(factors2.size()-1))==num){ //last element is at "factors2.size()-1"
origin=factors2.get(factors2.size()-1);
}
return origin;
/*
* Above logic works only for 5! but not other numbers ??
* */
}
i am doing one of the simple programin C, sum of digits of 5 digit number.Though i had done it using a simple function but i need to do it with recursion also.I had read many solution on net regarding this problem using recursion and had implemented one of mine.But that is giving error and i cant figure out what mesh i am doing in my algo.
#include<stdio.h>
int sum5(int x); //function for sum of digits of 5 digit number
int main()
{
int x;
int result;
printf("Enter a 5 digit number : ");
scanf("%d",&x);
printf("Number entered by you is %d",x);
result = sum5(x);
printf("Sum of digits of 5 digit number is = %d",&result);
return 0;
}
int sum5(int x)
{
int r;
int sum=0;
if(x!=0){
r=x%10;
sum=sum+r;
x=x-r; //doing this so that 0 come in the last and on diving it by 10, one digit will be removed.
sum5(x/10);
}
return sum;
}
but after its execution i am getting wrong result.It is dumping some anonymous value on the output.
Also, your sum5 function is incorrect. You have to add the value of sum5 to the sum variable of the caller function.
int sum5(int x)
{
int r;
int sum = 0;
if (x != 0) {
r = x % 10;
sum = r;
//x = x - r; - this isn't required. integer division will floor x
sum += sum5(x / 10);
}
return sum;
}
This is incorrect as it is printing the address of result and not its value:
printf("Sum of digits of 5 digit number is = %d",&result);
Change to:
printf("Sum of digits of 5 digit number is = %d", result);
Always check the result of scanf() to ensure a valid value was read:
/* Returns number of assignments made. */
if (scanf("%d", &x) == 1 && x > 9999 && x < 100000)
{
}
Plus the error in the implementation of sum5() as pointed out by Osiris
.