I'm creating a program that takes numbers from a separate .txt file and turns them into a factorial and then finds the amount of times each prime has occurred in the factorial.
I have been working on this code for the last few days and after many hours i keep getting stuck on the same thing, it will compile with no errors but when I try file redirection to get an input from another file it stops and acts like it is asking for input on an endless loop.
Any help would be much appreciated!
#include <stdio.h>
int Storage_array[5];
int current_prime, next_prime, factorial, prime_count, prime_number, num, factorial_current, factorial_next_num, current_number, total_number; //various variables used in program
int find_prime_count(int factorial, int prime_number); //3 functions declared below main()
int find_next_prime(int factorial, int current_prime);
int is_prime(int num);
int a, b, c; //utility varbles
main()
{
if((a=scanf("%d", &c)) == EOF) //checks to see if file is empty
{
return 0;
}
for(b=0; b<c; b++)
{
if((a=scanf("%d", &c)) == EOF) //checks to see if reached end of the file
{
break;
}
scanf("%d", &Storage_array[b]); //paces values from file into the storage array
printf("%d! = ", Storage_array[b]); //formatting the output
for(c=0;c<5;c++)
{
if(c=0) //creating a base case
{
current_prime = 2;
}
else
{
current_prime = next_prime; //updates the current prime after the base case
next_prime = find_next_prime(Storage_array[b], current_prime); //performes the outside functions that fine the next prime number within the factorial
prime_count = find_prime_count(Storage_array[b], current_prime); //performs outside function that finds the number of the current prime within the factorial
printf("(%d^%d)",current_prime ,next_prime); //prints the results in proper format
}
}
printf("\n"); //spaces between factorial numbers
}
}
int is_prime(int num) //function that determines if the input number is a prime number or not
{
for(a=2; a<num; a++) //base case knowing lowest prime is 2
{
if (num % a == 0) //determines if the number is a prime number using remainder when divided by various numbers
{
return a; //returns the number if it is prime
}
}
}
int find_prime_count (int factorial, int prime_number)
{
for(a=0;a<factorial;a++)//tests all possible prime numbers under the input factorial
{
if(a=0) //base case for lowest possible prime in factorial
{
factorial_current = 1;
factorial_next_num = 1;
}
else
{
factorial_current = (a*factorial_next_num);
}
if(factorial_current % prime_number == 0) //determines if the prime number fits into current factorial number
{
current_number = (factorial_current/prime_number); //finds number of times prime number fits in
total_number = (total_number + current_number); //adds total amount of times that prime has fit within the factorial
}
}
return total_number;
}
int find_next_prime (int factorial, int current_prime) //function that determines the next prime within the factorial
{
for(a=current_prime;a<factorial;a++) //checks all possible primes within the factorial
{
if (factorial % current_prime == 0) //determines if the number is a prime number using remainder when divided by various numbers
{
return a; //returns the number if it is next prime
}
}
}
the text file i have has the following content and is called input.txt
7
67
9
43
0
Related
This question already has answers here:
Why do we check up to the square root of a number to determine if the number is prime?
(13 answers)
Closed last year.
I wrote a program for finding prime numbers, my problem is that the code only will run correctly, only if I take the square root of the tested number (on the marked line).
When I have int i = input the code wont run.
I know that sqrt() is helpful here but i thought it should work without as well.
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
bool primeNumber(int input)
{
for(int i = sqrt(input); i > 1; i--) // <------- sqrt()
{
if(input % i == 0)
{
return false;
}
} return true;
}
int main()
{
int input;
printf("Pls type in a positive number: ");
scanf("%d",&input);
for(int i = input; i > 1; i--)
{
bool prime = primeNumber(i);
if(prime)
{
printf("%d is a prime number\n",i);
}
}
return 0;
}
Every positive integer has itself as a factor. A number is prime if its only factors are itself and one. If you consider a number non-prime because it has itself as a factor, then you will consider no numbers to be prime.
For starters the function is incorrect. It returns true for example for the number equal to 1 though 1 is not a prime number. Also as the function parameter has the signed integer type int then the user can pass to the function a negative number.
Also you should write a more general function that can work with any unsigned integer type.
As for your question
When I have int i = input the code wont run.
then each number is divisible by itself.
The function can be more efficient and written without using the function sqrt. Except the even number 2 all other prime numbers are odd. So there is no sense to check whether an even number except 2 is a prime number.
bool primeNumber( unsigned long long int input )
{
bool prime = number % 2 == 0 ? number == 2 : number != 1;
for ( unsigned long long int i = 3; prime && i <= number / i; i += 2 )
{
prime = number % i != 0;
}
return prime;
}
And in main you should write
unsigned int input;
printf( "Pls type in a non-negative number: ");
scanf("%u",&input);
for( unsigned int i = input; i > 0; i-- )
{
bool prime = primeNumber(i);
if(prime)
{
printf("%u is a prime number\n",i);
}
}
I am working on a program that takes a list of numbers from an input file and then takes the factorial of each number and finds the amount of each prime factor within the factorial. for example: if the input file just had one line that had "4" on it the program would take that file (input.txt) and then output the amount that each prime factor occurs which in this case is three 2's and one 3, (4! = 4*3*2*1 = 2*3*2*2 = (2^3)*(3^1) which would output the following:
4! = (2^3)*(3^1)
Every-time I compile the program it works fine but when i go to input the file ( ./a.out < input.txt) it returns "Floating point exception" and I have gone through the code and placed various printf statements to try and find which section of the code this error is occurring but it always returns the same thing and I was wondering if anyone can help me out. The code and input.txt are down below.
#include <stdio.h>
int Storage_array[5]; // array creating to store the values of the input text file (input.txt)
int current_prime, next_prime, prime_count, prime_number; // variables that involve the calculation and count of prime numbers
int factorial, factorial_current, factorial_next_num; //variables that invlove the handling of the input factorials
int num, current_number, total_number; //variables at help test if a number is prime of not
int find_prime_count(int factorial, int prime_number); //3 functions declared below main()
int find_next_prime(int factorial, int current_prime);
int is_prime(int num);
int a, b, c; //utility varbles
main()
{
if((a=scanf("%d", &c)) == EOF) //checks to see if file is empty
{
return 0;
}
for(b=0; b<c; b++)
{
if((a=scanf("%d", &c)) == EOF) //checks to see if reached end of the file
{
break;
}
scanf("%d", &Storage_array[b]); //paces values from file into the storage array
printf("%d! = ", Storage_array[b]); //formatting the output
for(c=0;c<5;c++)
{
if(c=0) //creating a base case
{
current_prime = 2;
}
else
{
current_prime = next_prime; //updates the current prime after the base case
next_prime = find_next_prime(Storage_array[b], current_prime); //performes the outside functions that fine the next prime number within the factorial
prime_count = find_prime_count(Storage_array[b], current_prime); //performs outside function that finds the number of the current prime within the factorial
printf("(%d^%d)",current_prime ,next_prime); //prints the results in proper format
}
}
printf("\n"); //spaces between factorial numbers
}
}
/* This function takes an integer as input and then determines if it is a prime number by finding the remainder when the input number is divided by all numbers leading up to the input number exept the number itself and 1, if it is a prime number it returns the prime number, if not it returns 0 */
int is_prime(int num) //function that determines if the input number is a prime number or not
{
for(a=2; a<num; a++) //base case knowing lowest prime is 2
{
if (num % a == 0) //determines if the number is a prime number using remainder when divided by various numbers
{
return a; //returns the number if it is prime
}
}
}
/* This function takes 2 integers as input (a factorial number and a prime number) and then counts the amount of times that prime number can fit into the factorial, if it can divide with a 0 remainder it divides the factorial by that amount and then stores the amount of times before going to next number within the factorial, after it has divided the entire factorial it returns the number of times the prime number fit */
int find_prime_count (int factorial, int prime_number)
{
for(a=0;a<factorial;a++)//tests all possible prime numbers under the input factorial
{
if(a=0) //base case for lowest possible prime in factorial
{
factorial_current = 1;
factorial_next_num = 1;
}
else
{
factorial_current = (a*factorial_next_num);
}
if(factorial_current % prime_number == 0) //determines if the prime number fits into current factorial number
{
current_number = (factorial_current/prime_number); //finds number of times prime number fits in
total_number = (total_number + current_number); //adds total amount of times that prime has fit within the factorial
}
}
return total_number;
}
/* This function takes 2 integers as input (a factorial and a prime number) and then determines the next prime number within the limits of the factorial by finding the next prime number and testing it to see if it is a factor of the factorial */
int find_next_prime (int factorial, int current_prime) //function that determines the next prime within the factorial
{
for(a=current_prime;a<factorial;a++) //checks all possible primes within the factorial
{
for(b=0;b<a;b++)
{
if (a % b == 0) //determines if the number is a prime number using remainder when divided by various numbers
{
return a; //returns the number if it is next prime
}
}
}
}
input.txt is below
5
46
89
36
0
My assignment is to check if a number is prime, but I have to use three sections to do it. The first is the main body of code and that is followed by two functions. The first checks if the number is even, and the second checks if it is prime. I know this is a rather tedious way to check if a number is prime but it is meant to get us introduced to functions and function calls!
UPDATE
I have gotten it all to work besides printing the smallest divisor of a non prime number. I thought using i from the second function would work but it will not output. I have copied by code below -- please help if you have any suggestions!
#include <stdio.h>
#include <math.h>
int even (int);
int find_div (int);
int main() {
int num, resultEven, resultPrime, i;
printf("Enter a number that you think is a prime number (between 2 and 1000)> \n");
scanf("%d", &num);
while (num < 2 || num > 1000) {
if (num < 2) {
printf("Error: number too small. The smallest prime is 2.\n");
printf("Please reenter the number > \n");
scanf("%d", &num);
}
else if (num > 1000) {
printf("Error: largest number accepted is 1000.\n");
printf("Please reenter the number > \n");
scanf("%d", &num);
}
else {
}
}
resultEven = even(num);
resultPrime = find_div(num);
if (resultEven == 1) {
printf("2 is the smallest divisor of %d. Number not prime\n", num);
}
else if (resultPrime == 1) {
printf("%d is the smallest divisor of %d. Number not prime\n", i, num);
}
else {
printf("%d is a prime number.\n", num);
}
return 0;
}
int even(int num) {
if (num % 2 == 0) {
return 1;
}
else {
return 0;
}
}
int find_div(int num) {
int i;
for (i = 2; i <= (num/2); i++) {
if (num % i == 0) {
return 1;
}
if (num == i) {
return 0;
}
}
return i;
}
I would create a function for Wilsons theorem (p-1)! = 1 (mod p) iff p is prime, first off, to make the functions nice and easy you will only need the one. for numbers less than 1000 it should work fine.
something like,
//it will return 1 iff p is prime
int wilson(int p)
{
int i, result = 1;
for (i = 0; i < p; i++)
{
result *= i;
result = result % p;
}
return result;
}
however if your not printing check that you have included, at the top of your file
#include <stdio.h>
your
resultEven = even(num)
needs a ; at the end but that was mentioned in the comments, besides that your methodology though odd is correct, also you do not need the empy else, that can simply be removed and your good
UPDATE:
//if return value == 1 its prime, else not prime, and
//return value = smallest divisor
int findDiv(int p)
{
int i= 0;
for (; i <= n/2; i++)
{
//you number is a multiple of i
if (num % i == 0)
{
//this is your divisor
return num;
}
}
//1 is the largest divisor besides p itself/smallest/only other
return 1;
}
your function call is correct but you need a semi colon (;) at the end of:
resultEven = even(num)
otherwise this program effectively checks for evenness. To check for prime one way is to ensure the number has no factors other than one and itself. This is done by finding the div of every whole number from 2 to half of the number being tested using a for loop. If a number produces a div of 0 then it is not prime because t has a factor other than 1 and itself.
so my task is as follows: Construct a do-while() loop, which continues to prompt the user for an integer, and determines the sum of integers entered, until a prime number is encountered. The prime number should not be included in the sum. Show all variable declarations.
I have all of the variable add up correctly however cannot seem to get the function to stop on a prime number. To try to correct this I made the variable "primecheck" and set it to 2++ thinking that it would be every integer above 2 (obviously not possible but one could hope). any assistance would be much appreciated!
int main (void)
{
int sum = 0, num = 0, i = 0, primecheck = 0, two = 2;
primecheck = two++;
do
{
printf ("Enter an integer: ");
scanf ("%d", &num);
if (num % primecheck == 0 && primecheck != num)
{
sum += num;
}
} while (num % primecheck == 0 && primecheck != num);
i = sum;
printf("%s%d%s", "Sum = ", i, "\n");
}
One possibility would be to introduce a function which performs the primality check, which could be done by using check divisions by all smaller numbers and terminate the loops as soon as a prime number is found. An implementation can be found following this link; the code can be refactored to the follwing function for primality testing. The function returns 1 if n is prime and 0 otherwise. The implementation uses an explicit while loop as the requirements apparently demands it.
int is_prime(int n)
{
int i=3;
int flag=0;
if (n%2==0)
{
return 0;
}
do
{
if (n%i==0)
{
flag=1;
break;
}
i+=2;
}
while (i*i<=n);
return flag;
}
I wrote a code to get first 1000 prime palindromes ,though my logic is correct ,I dont seem to be getting first 1000 prime palindromes ,I am getting some 113 Prime Palindromes and after that I don't get any . I think this is because my logic is not efficient enough ,that is why it is taking so much time to compile ,but I already tried three different methods and everytime the Runtime is getting stuck after the 113th Prime Palindrome Number .
Can anyone explain why exactly I am getting this problem ,is it because the code is not efficient?
/* Program to find the first 1000 prime palindromes */
#include<stdio.h>
#include<math.h>
int prime(long int n)
{
int i,check=0;
if(n!=2 && n%2==0)
return 0;
if(n==2 || n==3)
return 1;
for(i=3;i<n/2;i=i+2)
if(n%i==0)
check++;
if(check==0)
return 1;
else
return 0;
}
/*long int reverse_number(long int n,long int partial)
{
if(n==0)
return partial;
else
return reverse_number(n/10,partial*10 + n%10);
}*/
int palindrome(long int n)
{
long int reverse = 0;
long int n_copy = n;
int rem;
while(n_copy!=0)
{
rem = n_copy%10;
reverse = reverse*10;
reverse = reverse + rem;
n_copy = n_copy/10;
}
if(reverse==n)
return 1;
else
return 0;
}
int main()
{
long int i;
int count=5,digits;
printf("The 1000 prime palindromes are: \n");
printf("1. 2\n2. 3\n3. 5\n4. 7\n");
for(i=11;;i=i+2)
{
if(prime(i))
{
if(palindrome(i))
{
printf("%d. %ld\n",count,i);
count++;
}
/*if(reverse_number(i,0)==i)
{
printf("%d. %ld\n",count,i);
count++;
}*/
}
if(count==50)
break;
}
printf("\n\n");
return 0;
}
Let me quote my imperative programming professor here: "You can check whether the number is a prime and a palindrome, or check whether it's a palindrome and a prime..."
Also, you're breaking the loop when count is 50, which I suppose you want to do when it's 1000.
Without editing the prime and palindrome functions, besides the order in which they're called, my PC stops finding more after 781 9989899.