Find Prime gcd between two numbers - c

For a student course in c,
I need to find the prime greatest common divisor (gcd) of two integers.
If there is no answer the output should be 1.
You can only use if statement, scanf, loops (no external functions).
Examples of inputs and outputs:
(20,20)--->5
(21,20)--->1
(22,20)--->2
(29,29)--->29
Can someone please help me with this?
Here is what I have so far:
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is a factor of both number */
hcf=i;
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}
There are a lot of examples on how to find the gcd but none that I have found for the prime gcd.

sample of fix
#include <stdio.h>
int main(void){
int num1, num2, i, hcf = 1;
int tmp1, tmp2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
tmp1 = num1;
tmp2 = num2;
for(i=2; i<= tmp1 && i<= tmp2; ++i){
while(tmp1 % i== 0 && tmp2 % i == 0){
hcf=i;
tmp1 /= hcf;
tmp2 /= hcf;
}
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}

You could just do it the straightforward way:
Step one: Generate a list of all prime numbers less than the maximum int value.
Step two: Use that list, and trial division, to find the prime factors of each of your given integers. Keep the list of prime factors for each.
Step three: go through one list of factors, and check each to see if it's in the other list. If only one is, that's your largest common prime divisor. If more than one is in both lists, the GCD is not prime.

import java.util.Scanner;
public class Main
{
static boolean CheckPrime(int n)
{
int flag=0;
if(n==0 || n==1)
{
flag=0;
}
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();
int[] arr1 = new int[n1];
int[] arr2 = new int[n2];
for(int i=2;i<=n1;i++)
{
if(n1%i==0)
{
if(CheckPrime(i))
{
arr1[i-2]=i;
}
}
}
for(int j=2;j<=n2;j++)
{
if(n2%j==0)
{
if(CheckPrime(j))
{
arr2[j-2]=j;
}
}
}
int max=-1;
for(int i=1;i<n1;i++)
{
for(int j=1;j<n2;j++)
{
if(arr1[i]==arr2[j])
{
if(max<arr2[j])
{
max=arr2[j];
}
break;
}
}
}
if(max==0){max=1;}
System.out.print(max);
}
}

Related

Function returning garbage values but not all of them in C

So here's what I need to accomplish:
Enter a sequence of whole numbers that end with 0
Out of those numbers - calculate its square root and check if it's an odd or even number.
Make use of while or do-while loops in the process.
I have my no. of elements set to 4 for now, but no matter what number I set, roughly half of them come out as garbage values in the end.
I assume this is a pointer related issue or a memory leak somewhere, I'm just not sure. Any input would be appreciated
int num_of_elements = 4;
//function declarations
int* check_num(int i, int number[num_of_elements]);
//main function
int main() {
//local variables
int num[5];
int i;
printf("\nEnter a number that ends with 0");
for (i=0;i<num_of_elements;i++) {
do {
printf("\n%d.Enter a number: ", i+1);
scanf("%d", &num[i]);
} while (num[i] % 10 != 0);
printf("\nElement %d: %d\n", i+1, num[i]);
}
int j = 0;
while (j<num_of_elements) {
check_num(j, &num[j]);
j++;
}
return 0;
}
//function that takes the square root and checks if it's an odd or even number
int* check_num(int j, int number[num_of_elements]) {
int val;
printf("\nnumber[%d] is %d",j,number[j]);
val = sqrt(number[j]);
printf("\nsquare root is is %d", val);
if (val % 2 == 0) {
printf("\n%d is a an even number squared", number[j]);
} else {
printf("\n%d is an odd number squared", number[j]);
}
printf("\n");
return number;
}
For what I can see, there is only one error:
The call to check_num must be changed from check_num(j, &num[j]); to check_num(j, &num); otherwise you are passing for example the address num[j] and from that address you are trying to access the j element, so you are trying to access the j+j element.
you are getting that int array into an int array variable. Instead of that , you can directly give it to the int variable.
see this code.
int num_of_elements=4;
//function declarations
void check_num(int i, int number);
//main function
int main() {
//local variables
int num[5];
int i;
printf("\nEnter a number that ends with 0");
for (i=0;i<num_of_elements;i++) {
do {
printf("\n%d.Enter a number: ", i+1);
scanf("%d", &num[i]);
} while (num[i] % 10 != 0);
printf("\nElement %d: %d\n", i+1, num[i]);
}
int j = 0;
while (j<num_of_elements) {
check_num(j,num[j]);
printf("%d\n",num[j]);
j++;
}
return 0;
}
void check_num(int j, int number) {
int val;
printf("\nnumber[%d] is %d",j,number);
val = sqrt((number));
printf("\nsquare root is %d", val);
if (val % 2 == 0) {
printf("\n%d is a an even number squared", number);
} else {
printf("\n%d is an odd number squared", number);
}
printf("\n");
//return number;
}

What is the shortest way to count prime numbers from an array. All normal primality test are not passing the test cases because of time limit

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;
}

Twin primes C code

I'm trying to print the following series 4 6 12 18 30 42 60 72....up to n in C language. Its logic is simple; we have to print the number such that the preceding and successive number should be prime! But the following code is not looping after printing the 4. What's wrong in the following code?
#include <stdio.h>
int main(){
int n, i, j, p2, k;
int count1=0, count2=0;
printf("enter the number:\n");
scanf("%d",&n);
for(i=3;i<n;i++){
for(j=2;j<i;j++){
if(i%j==0){
count1++;
break;
}
}
p2=i+2;
for(k=2;k<i;k++){
if(p2%k==0){
count2++;
break;
}
}
if(count1==0 && count2==0){
printf("%d",i+1);
}
}
}
You just need to set counters to 0 at the end of the loop
#include<stdio.h>
int main(){
int n, i, j, p2, k;
int count1=0, count2=0;
printf("enter the number:\n");
scanf("%d",&n);
for(i=3;i<n;i++){
for(j=2;j<i;j++){
if(i%j==0){
count1++;
break;
}
}
p2=i+2;
for(k=2;k<i;k++){
if(p2%k==0){
count2++;
break;
}
}
if(count1==0 && count2==0){
printf("%d ",i+1);
}
count1=0; count2=0;
}
}
your code is right, just set count1 and count2 to 0 at the end of the outer for loop.
you can try it this way too.
this code is in Java. you can convert it to C . Logic remains the same.
for Arraylist take arrays of fixed length equal to n.
import java.util.*;
class prime
{
public static void main(String[] args){
int n, i, j, p2, k,o;
ArrayList<Integer> prime = new ArrayList<Integer>();
ArrayList<Integer> series = new ArrayList<Integer>();
int count1=0, count2=0;
Scanner s = new Scanner(System.in);
System.out.println("enter the number:\n");
n=s.nextInt();
if(n<3)
{
System.out.println("prime numbers start from 3");
}
for(i=3;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
count1=1;
break;
}
}
if(count1==0)
{
prime.add(i);
}
count1=0;
}
for(k=0;k<prime.size()-1;k++)
{
int prdsr=prime.get(k);
int sucsr=prime.get(k+1);
if((sucsr-prdsr)==2)
{
series.add((prdsr+1));
}
}
for(o=0;o<series.size();o++)
{
System.out.print(" "+series.get(o));
}
}
}

C programming: Find the subtraction of two greatest numbers out of three

I'm starting to learn the c programming language and I have to write a program that subtracts two greatest numbers out of the three entered numbers. Can anyone help me with this?
Edit: Sorry, I still don't know how this site functions...
I know how to find the greatest number, but I'm not sure how to find the other one.
#include<stdio.h>
int main()
{
int a, b, c, d, max;
printf("Enter three numbers: ");
scanf("%d%d%d%d", &a, &b, &c);
max=a;
if (max<b) {max=b;}
if (max<c) {max=c;}
printf("the greatest number is %d\n", max);
return 0;
}
Don't expect to get answers to questions like yours. Try this. The assumption is that you are dealing with positive integers only. If you need to consider negative integers as well, you can do it yourself. You should note that this is the not the best solution, there can be much more elegant ones.
#include <stdio.h>
int main() {
int numbers[3];
printf("Enter number 1: \n");
scanf("%d", &numbers[0]);
printf("Enter number 2; \n");
scanf("%d", &numbers[1]);
printf("Enter number 3: \n");
scanf("%d", &numbers[2]);
printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);
int maximum_0 = 0;
int maximum_1 = 0;
int i;
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_0) {
maximum_0 = numbers[i];
}
}
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_1 && numbers[i] < maximum_0) {
maximum_1 = numbers[i];
}
}
printf("Result: %d\n", (maximum_0 - maximum_1));
return 0;
}
//Here it is since you are beginner Without loops
#include<stdio.h>
int main()
{
int a, b, c, d, max,min,result;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b)
{
if(a>c)
{
max=a;
if(c>b)
min=c;
else
min=b;
}
else
{
max=c;
a=min;
}
}
else
{
if(b>c)
{
max=b;
if(a>c)
min=a;
else
min=c;
}
else
{
max=c;
min=b;
}
}
result=max-min;
printf("the greatest number is %d\n", result);
return 0;
}
Use an array and then use qsort to sort your values. The code below will do this for you. The order of the array will be such that the first element will have your smallest number and the last element your largest.
#include<stdio.h>
void sort(const void* d1, const void* d2)
{
int a = *(int*)d1;
int b = *(int*)d2;
if (a > b)return 1;
if (b > a)return -1;
return 0;
}
int main()
{
int abc[3];
printf("Enter three numbers: ");
scanf("%d%d%d", &abc[0], &abc[1], &abc[2]);
qsort(abc, 3, sizeof(int), sort);
printf("%d\n", abc[2] - abc[1]);
while (1){}
return 0;
}

C Segmentation Fault With Printf() and Scanf()

I'm new to C, and I'm getting a segmentation fault that I can't understand. I have the following program, which attempts to calculates the number of factors of a strictly positive number:
#include <stdio.h>
#include <math.h>
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
int check (int x) {
if (x>0) {
return 1;
} else {
return 0;
}
}
int main(void) {
int number;
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
}
return 0;
}
The segmentation fault occurs immediately after entering an integer and ENTER after these lines in main():
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
What in these lines is causing the segmentation fault, and what can I do to avoid it?
Your recursion doesn't stop if you try to divide out factor one.
Just let factor never be 1:
for (i=2; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
I should say WHY it segfaults: It's because every function call pushes the current program counter (the position in your program where you currently stand) and function arguments on the stack (aka call stack), where the stack is a relatively small memory block used for, well, function calling and local variables.
So if you are pushing your stack too hard, it will fall over. End of game, aka segfault ;)
You probably have a problem with this recursion:
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
Change your numberOfFactors to something like:
int numberOfFactors (int number)
{
int i=1;
int ret=0;
for(;i<=number;i++) {
if (number%i == 0) {
ret++;
}
}
return ret;
}
And then, change this portion:
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
}
To something simpler like:
if (check(number)){
factors = numberOfFactors(number);
printf("%d^%d ", number, factors);
}

Resources