I am trying to write a program in C. The program is supposed to find the GCD (greatest common divisor) of a given array. I am trying to use the smallest number of the array to find the GCD. I was wondering whats wrong with my last loop. I havent figured a way on how to check if the division is giving any decimal points in order to stop the loop. This is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int i;
int j;
int minimum = A[0];
int GCD;
int temp;
for (i=1;i<9;i++)
{
if( A[i] < minimum)
{
minimum = A[i];
}
}
for (i=1; i < minimum/2; i++)
{
for (j = 0; j < 9;j++)
{
GCD = 2*i;
temp = ((A[j])/(GCD));
int check = temp%1;
if (check == 0)
break;
}
}
printf("The Greates Common Denominator is: %d", GCD);
return 0;
}
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y){
unsigned wk;
if(x<y){ wk=x;x=y;y=wk; }
while(y){
wk = x%y;
x=y;
y=wk;
}
return x;
}
int gcd_a(int n, int a[n]){
if(n==1) return a[0];
if(n==2) return gcd(a[0], a[1]);
int h = n / 2;
return gcd(gcd_a(h, &a[0]), gcd_a(n - h, &a[h]));
}
int main(void){
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int size_A = sizeof(A)/sizeof(*A);
int gcd = gcd_a(size_A, A);
printf("%d\n", gcd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b)
{
int r;
while(a)
{
r=b%a;
b=a;
a=r;
}
return b;
}
int gcd_(int* A, int N)
{
int c=gcd(*A,*(A+1));
int i,g;
for(i=1;i<N-1;i++)
{
g=gcd(c,*(A+1+i));
c=g;
}
return c;
}
int main(void)
{
int A[]={96,60,32,72,84,90};
int N=sizeof(A)/sizeof(A[0]);
int t=gcd_(A,N);
printf("%d",t);
return 0;
}
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return(0);
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}
int main(void)
{
int A[10] = { 112, 160, 180, 240, 288, 32, 480, 96, 60, 72 };
int g = A[0];
for (int i = 1; i < 10; i++)
g = gcd(g, A[i]);
printf("The Greatest Common Denominator is: %d\n", g);
return 0;
}
The answer is 4. This is clearly correct; it is the GCD of 32 and 60; everything else is divisible by 4.
If desired, you could optimize the loop with:
for (int i = 1; i < 10 && g != 1; i++)
g = gcd(g, A[i]);
When the GCD is 1, it won't get any bigger,
#include <iostream>
using namespace std;
int gcd(int a, int b){
int t;
while(a)
{
t = a;
a = b%a;
b = t;
}
return b;
}
int main(){
int n;
cout<<"how many numbers (Max 10): "; // your choice
cin>>n;
cin.ignore();
cout<<endl;
int arr[n-1];
for (int i = 0; i < n; ++i) {
cin>>arr[i];
}
int ans;
ans = arr[0];
for (int j = 0; j < n; ++j) {
ans = gcd(ans,arr[j]);
}
cout<<"GCD = "<<ans;
//cin.get();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, flag = 0, small, *data;
/* get the number of inputs from the user */
printf("Enter the number of inputs:");
scanf("%d", &n);
/* allocate memory to store n numbers */
data = (int *)malloc(sizeof(int) * n);
/* get n numbers from the user */
for (i = 0; i < n; i++) {
printf("Data[%d]: ", i);
scanf("%d", &data[i]);
}
/* find the smallest of n numbers */
small = data[0];
for (i = 1; i < n; i++) {
if (data[i] < small)
small = data[i];
}
/*
* use the smallest no to find gcd of n numbers.
* Start checking from small to 1 whether the
* same value divides all the given inputs
*/
for (i = small; i > 0; i--) {
for (j = 0; j < n; j++) {
if (data[j] % i != 0) {
flag = 1;
}
}
/* print the result */
if (!flag) {
printf("GCD of given %d numbers is %d\n", n, i);
break;
}
flag = 0;
}
return 0;
}
#include <stdio.h>
#include <conio.h>
int x;
void main()
{
int i,num[10]={0};
printf("How many numbers do you want to enter =");
scanf("%d",&x);
for(i=0;i<x;i++)
scanf("%d",& num[i]);
printf("The hcf of the numbers are = %d ", hcf(num));
}
int hcf(int num[])
{
int count,rem,lv,i,j;
lv=gnum(num);
if(lv==1)
return 1;
for(j=2;j<=lv;j++)
{
count =0;
for(i=0;i<x;i++)
{
rem = num[i]%j ;
if( rem != 0 || num[i]< j )
break;
else
{
count++;
}
}
if(count == x)
{
for(i=0;i<x;i++)
num[i]=num[i]/j;
return(j*hcf(num));
}
}
if(count!= x)
return 1;
}
int gnum(int num[])
{
int i,temp=num[0];
for(i=0;i<x;i++)
{
if(temp >= num[i])
temp = num[i];
}
return (temp);
}
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
int A[2] = {10,30};
int a,b,r,i;
for ( i = 1; i < 2; i++)
{
a=A[0],b=A[i];
while(b!=0)
{
r=a%b;/*remainder*/
a=b;
b=r;
}
}
printf("The Greatest Common Denominator is: %d\n", a);
return 0;
}
/*You can change the **for** loop range according to the array values*\
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int i;
int j;
int minimum = A[0];
int GCD;
int temp;
int f;
for (i=1;i<10;i++)
{
if( A[i] < minimum)
{
minimum = A[i];
}
}
for (i=1; i <= minimum; i++)
{
f=0;
for (j = 0; j < 10;j++)
{
if(A[j]%i!=0)
{
f=1;
break;
}
}
if(f==0)
GCD=i;
}
printf("The Greates Common Denominator is: %d ", GCD);
return 0;
}
enter code here
Related
I have written a piece of code below, that ask a user input a number N (3 < N < 1.000.000) odd and count the number of prime number pairs whose sum is equal to N. That code works, but I need to optmize it to make more efficient. When the user inputs a number, I count the number of primes up to this number and store each prime number in a vector, and after that count the number of prime number pairs whose sum is equal to this input.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void seleciona_primos (int *vet, int n, int raiz){
int i, j;
vet[2] = 2;
for(i=3; i<=n; i+=2){
vet[i] = i;
}
for (i=3; i<= raiz; i+=2){
if(vet[i]!=0){
for(j=3; j<=n; j+=2){
if ((vet[i]!=vet[j]) && (vet[j]%vet[i] == 0)){
vet[j]=0;
}
}
}
}
}
int conta_pares (int *vet, int n){
int i, j, count=0;
for (i=3; i<=n; i+=2){
if(vet[i]!=0){
for(j=3; j<=n/2; j+=2){
if((vet[j]!=0) && (vet[i] + vet[j] == n)&& (vet[i]!=0)){
//printf("%d ", vet[i]);
//printf("%d\n", vet[j]);
count++;
vet[i] = 0;
}
}
}
}
if(vet[n-2]!=0){
count++;
}
return count;
}
int main()
{
int *vet, n, raiz, i , count;
scanf("%d", &n);
vet = (int *) calloc((n+1), sizeof(int));
raiz = floor(sqrt(n));
seleciona_primos(vet, n, raiz);
count = conta_pares(vet, n);
printf("%d",count);
//for(i=3; i<=n; i+=2){
//if(vet[i]!=0){
//printf("%d\n", vet[i]);
//}
//}
return 0;
}
I make an array who countain the prime numbers from 2 to N(1 has only one divisors and 0 has infinites of divisors), and then I check if two numbers from the array equal to N
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool Prime(int);
int main()
{
long N;
do
{
printf("Give me the number of numbers :");
scanf("%ld",&N);
}while(N<=4||N>1000000);//N must be between 3 & 1000000
int prime[N];
int arr[N];
int j=0;
for(int i=2;i<N;i++)
{
if(Prime(i)==true)
{
prime[j]=i;
j++;
}
}
printf("\n\n");
for(int p=0;p<j-1;p++)
{
for(int q=p+1;q<j;q++)
{
if(N==prime[p]+prime[q])
{
printf("\n%d = %d + %d \n",N,prime[p],prime[q]);
}
}
}
printf("\n\n");
return 0;
}
bool Prime(int n)
{
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
Example :
Input : N =100;
Output :
100 = 3 + 97
100 = 11 + 89
100 = 17 + 83
100 = 29 + 71
100 = 41 + 59
100 = 47 + 53
Thank you for all suggestions!!
And after some research and tries, I have found the solution that follow bellow:
int isPrime(int x)
{
int i;
for(i = 2; i <= sqrt(x); i++)
{
if(x%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int i, count = 0, n ;
scanf("%d", &n);
for(i = 3; i <= n/2 ; i+=2)
{
if(isPrime(i))
{
if((isPrime(n-i)))
{
count++;
}
}
}
if(isPrime(n-2))
{
count++;
}
printf("%d", count);
return 0;
}
I modified your code to optimize it, I have set two Boolean vectors one for 5 mod(6) prime numbers (vet1[i]=false corresponds to prime number 6 x i-1 es. i=1 corresponds to prime number 5=6 x 1-1) and one (vet2[i]corresponds to prime number 6 x i+1) for prime numbers 1 mod(6)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void seleciona_primos (bool *vet1, bool *vet2,int n){
int i, i1,imax;
imax=(n-n%6)/6+1;
for (i=1; (6*i-1)*(6*i-1) <=n; i++){
if (vet1[i]==false){
for(int i1 = 6*i*i; i1 <= imax+2*i; i1 += (6*i-1)){
if(i1<imax)
vet1[i1]=true;
vet2[i1-2*i]=true;
}
}
if (vet2[i]==false){
for(int i1 = 6*i*i; i1 <= imax; i1 += (6*i+1)){
vet1[i1]=true;
if(i1<imax-2*i)
vet2[i1+2*i]=true;
}
}
}
}
int conta_pares (bool *vet1, bool *vet2, int n){
int i,imax, count=0,r6;
imax=(n-n%6)/6;
r6=n%6;
if (r6==0){
for (i=1; i<imax; i++){
if(vet1[i]== false && vet2[imax-i]== false)
count++;
}
}
if (r6==2){
for (i=1; i<imax; i++){
if(vet2[i]== false && vet2[imax-i]== false)
count++;
}
count=(count+count%2)/2;
}
if (r6==4){
for (i=1; i<=imax; i++){
if(vet1[i]== false && vet1[imax+1-i]== false)
count++;
}
count=(count+count%2)/2;
}
if (r6>0 && r6<3){
if (vet1[imax]==false)
count++;
}
if (r6>2 && r6<5){
if (vet2[imax]==false)
count++;
}
return count;
}
int main()
{
int n, i , count;
bool *vet1, *vet2;
scanf("%d", &n);
vet1 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
vet2 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
seleciona_primos(vet1,vet2, n);
count = conta_pares(vet1,vet2, n);
printf("%d",count);
free(vet1);
free(vet2);
return 0;
}
I am making this code for a random dice roller and I want to get a random number until I get three 6's on the dice. For some reason this produces the same number for all runs, but on my for loop it works. Can I get some help getting this to work?
The pesky While loop in question:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Ran(int max) {
return (rand() % max)+ 1;
}
int main(){
int max = 6;
int nsuc = 0, ncount = 0;
int matt = 40;
srand((unsigned) time(0));
for(int i = 1; i <= 20; i++){
while(!(nsuc >= 3)){
int nr = Ran(max);
if(nr < matt){
ncount++;
if(nr == 6){
nsuc++;
}
}
}
printf("Number of rolls until 3 6's = %d\n", ncount);
}
}
Cleaned up the for loop so it's easier to read now:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Shuffle() {
srand((unsigned) time(NULL));
}
int Ran(int max) {
return (rand() % max)+ 1;
}
void binomial(int run, int max, int btrials, int *array, int scount){
for(int j = 1; j<=run; j++){
for (int i=1;i<=btrials;i++){
int r = Ran(max);
if(r == 6){
scount++;
}
printf("Trial %i, roll = %d\n",i , r);
}
array[j-1] = scount;
scount = 0;
printf("\nEnd of Run %d\n\n",j);
}
}
int main(){
int Runs;
printf("How many runs do you want to do? > ");
scanf("%d", &Runs);
printf("\n");
int bdist[20];
int distcount = 0;
int max = 6;
int btrials = 20;
int suc[Runs+1];
int scount = 0;
Shuffle();
binomial(Runs, max, btrials, suc, scount);
printf("All counts of succesful 6 rolls.\n");
/*
for(int z = 1; z<=Runs; z++){
printf("Run %d has %d 6's\n", z, suc[z-1]);
}
*/
for(int k = 0; k<=btrials; k++){
for(int j = 1; j<=Runs; j++){
if(suc[j-1] == k){
distcount++;
}
}
bdist[k] = distcount;
distcount = 0;
}
for(int t = 0; t<=btrials; t++){
printf("Number of runs with %d 6s = %d\n", t, bdist[t]);
}
return 0;
}
You never reset nsuc or ncount in the first version with the while loop. After the first time thru, they still have their values from the previous loop so you get the same output.
Not an answer strictly related to the question, but something I noticed.
If I recall, generating ranged random numbers using the (rand % max)+ 1 idiom is not advised.
One.
Two
I have make this program that calculates number factorization such as 60 = 2^2 * 5 * 3.
How can i modify my code in order to print POWERFUL NUMBERS such as 9000 = 2^3 * 3^2 * 5^3 without using math.h library and without using arrays?
Thank you very much!!
#include<stdio.h>
#define MAX 1000
int main(){
int num;
int counter;
int number;
char factorizationOutput;
int isAchiles = 0;
int factor=2;
for(counter=2;counter<=MAX;counter++){
isAchiles = 1;
number=counter;
int factor=2;
while(factor<number){
int power=0;
if(number%factor==0){
while(number%factor==0){
number=number/factor;
power++;
}
if(power == 1){
isAchiles = 0;
}
printf("%d^%d",factor,power);
if(number!=1)
printf(" X ");
}
factor++;
}
if(number!=1)
printf("%d^1.\n",factor);
if(isAchiles == 1){
printf("factorazation of number %d is: ",counter);
}
}
}
#include<stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
printf("%d = ", n);
for(int i = 1; i <= n; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
int l = 0;
if(count == 2)
{
while(n % i == 0)
{
l++;
n = n/i;
}
printf("%d^%d*", i, l);
}
}
}
Program for the frequency of a number
Please help me with this code to get clear output. I am a beginner
I have made the program using an array. I don't know whether it is correct or not. Made with my own logic
int count(int a)
{
int c;
while(a>=1)
{
c++;
a=a/10;
}
return c;
}
int main()
{
//program to find frquency of the number
int a,n,d;
int b[100];
int e[100];
scanf("%d",&a);
n=count(a);
for(int i=n;a>0;i--)
{
b[i]=a%10;
a=a/10;
}
for(int i=1;i<=n;i++)
{
d=b[i];
e[d]++;//most probably this part error occurs
printf("%d\n",d); //used this this to confirm that i have correctly stored value in d.
}
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
return 0;
}
The line int c; should be int c = 0;
The line int e[100]; should be int e[100] = {0};
The following code could work:
#include <stdio.h>
int count(int a) {
int c = 0;
while (a >= 1) {
c++;
a = a / 10;
}
return c;
}
int main() {
// program to find frquency of the number
int a, n, d;
int b[100];
int e[100] = {0};
scanf("%d", &a);
n = count(a);
for (int i = n; a > 0; i--) {
b[i] = a % 10;
a = a / 10;
}
for (int i = 1; i <= n; i++) {
d = b[i];
e[d]++; // most probably this part error occurs
printf("%d\n", d); // used this this to confirm that i have correctly
// stored value in d.
}
for (int i = 1; i <= n; i++) {
printf("%d ", e[i]);
}
return 0;
}
Also, you can do it use snprintf:
#include <stdio.h>
int main() {
int a;
int max = -1;
char buf[100];
int count[10] = {0};
scanf("%d", &a);
snprintf(buf, sizeof(buf), "%d", a);
for (int i = 0; buf[i] != '\0'; ++i) {
int temp = buf[i] - '0';
++count[temp];
if (temp > max)
max = temp;
}
for (int i = 0; i <= max; ++i)
printf("%d ", count[i]);
return 0;
}
So I would like to know how to write a non-recursive function to print all permutations given an N and r where r^N gives you the total number of permutations.
Example: N = 3, r = 2, total permutations = 8
output:
000
001
010
011
100
101
110
111
This is what I tried but of course it only works for one case:
void perm_iter(int N, int nr_vals){
int pos = N-1;
int i,j,k;
int P_array[N];
for(i=0;i<N;i++){
P_array[i] = 0;
}
int val_array[nr_vals];
for(i=0;i<nr_vals;i++){
val_array[i] = i;
}
do{
for(i=0;i<N;i++){
for(j=0;j<nr_vals;j++){
P_array[pos-1] = val_array[j];
for(k=0;k<nr_vals;k++){
P_array[pos] = val_array[k];
for(i=0;i<N;i++){
printf("%d",P_array[i]);
}
printf("\n");
}
}
pos--;
}
}while(pos > 0);
}
This is an odometer function with variable radix, not really a permutation.
#include <stdio.h>
#include <stdlib.h>
void show(int *a, int n)
{
int i;
for(i = 0; i < n; i++)
printf("%1d", a[i]);
printf("\n");
}
void gen_all_numbers(int r, int n)
{
int i;
int *a;
if(r < 2 || n < 1) /* parameter check */
return;
r -= 1; /* r = max digit value */
a = malloc(n * sizeof(int));
for(i = 0; i < n; i++) /* start with all zeroes */
a[i] = 0;
show(a, n);
while(1){
i = n - 1;
while(a[i] < r){ /* increment last digit */
a[i]++;
show(a,n);
}
/* find next digit to increment */
while(i >= 0 && a[i] == r)
i--;
if(i < 0)break; /* return if done */
a[i]++;
while(++i < n) /* zero following digits */
a[i] = 0;
show(a,n);
}
free(a);
}
int main()
{
gen_all_numbers(2,4);
return 0;
}
This works. Just make sure you use the -lm option when compiling or you'll get an error about the pow function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void perm(double r,double N){
double num=pow(r,N);
int count=(int)num;
int n=0,b=0;
for (n=0;n<count;n++){
printf("%d: ",n);
for (b=2;b>=0;b--){ //go through bits 2 to 0.
if (n & (1<<b)){printf("1");}else{printf("0");}
}
printf("\n");
}
}
int main(){
perm(2,3);
return 0;
}