Twin primes C code - c

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

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

Chef and Dolls MISSP

Here is the link to the codechef problem - Chef and Dolls
Why does my code always print the wrong answer? The output should be the number which doesn't have a match but my code always print the first element. Coudn't get the conditions right, what should be the condition?
Problem Statement
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Example
Input:
1
3
1
2
1
Output:
2
#include <stdio.h>
int main(){
int t, N, i, m, k, z, flag=0;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d", &arr[i]);
}
int j;
for(j=0;j<N;j++){
m=arr[j];
for(k=0;k<N;k++){
if(m==arr[k] && k!=j)
{
flag=0;
}
else
{
flag=1;
break;
}
printf("%d", m);
}
}
}
return 0;
}
You break out of your loop everytime k==j. You have to check for every array member if it is present somewhere else in the array.
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
for(j=0;j<N;j++)
{
m=arr[j];
flag=1;
for(k=0;k<N;k++)
{
if(m==arr[k] && k!=j)
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
}
}
}
return 0;
}
But be careful as this only looks if an array member has a duplicate in it. You probably want to check if there is an even number of elements in the array.
If you want to check which array members occur an odd number of times
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, num, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N], odds[N], n_odds;
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
n_odds=0;
for(j=0;j<N;j++)
{
m=arr[j];
num=0;
for(k=0;k<N;k++)
{
if(m==arr[k])
{
num++;
}
}
if(num%2) //occurs an odd number of times
{
flag=1;
for(i=0;i<n_odds;i++)
{
if(m==odds[i]) //has already been checked
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
odds[n_odds++]=m;
}
}
}
}
return 0;
}

Why this program is having Segmentation fault (core dumped) in line 62: 13

Problem statement:
Given N sticks each of different integral length Li, find the probability that you can make a quadrilateral taking 4 sticks at random (not paying attention to the order) from the given N sticks. (Degenerate quadrilaterals not included.)
Constraints:
1 <= M<=10
3 <= N <= 1000
1 < = Li < = 1500
Code:
#include<stdio.h>
int gcd(int n1,int n2) // calculate GCD
{
if(n1==0||n2==0) return 0;
if(n1==n2) return n1;
if(n1>n2) return gcd(n1-n2,n2);
return gcd(n1,n2-n1);
}
int main(void)
{
int m; // test cases
scanf("%d",&m);
while(m--)
{
int n;
scanf("%d",&n);
int a[1501];
int b[n];
int i,val,k;
for(i=1;i<=1500;i++) a[i]=0; //initialization
for(i=0;i<n;i++) //taking values
{
scanf("%d",&val);
a[val]=1;
}
int j=0,l,tcase=0,fcase=0; //truecase and falsecase
for(i=1;i<=1500;i++) //making another array of length n
{
if(a[i]==1)
{
b[j]=i;
j++;
}
}
if(n<4) printf("0/1\n");
else
{
for(i=0;i<=n-4;i++)
{
for(j=i+1;j<=n-3;j++)
{
for(k=j+1;k<=n-2;k++)
{
for(l=k+1;l<=n-1;l++)
{
if((b[i]+b[j]+b[k])>b[l]) tcase++;
else
{
fcase=fcase+(n-l);
l=n;
}
} //line: 62
}
}
}
int ttcase=tcase+fcase; //total possible cases
int div=gcd(tcase,ttcase); //to make a/b form
if(div==0) printf("0/1\n");
else
{
tcase=tcase/div; //to make a/b form
ttcase=ttcase/div; //to make a/b form
printf("%d/%d\n",tcase,ttcase);
}
}
}
return 0;
}

Find Prime gcd between two numbers

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

Project Euler number 37

Here's the problem statement:
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
My code gives me a partial output. Only 5 or 6 of the eleven required primes are being outputted, 3797 not being one of them. So to find the error, I manually (on a piece of paper) ran the code for 3797 and somehow can't manage to find the glitch.
I think the error is in the second part, the part of code which checks whether the number is truncatable from the left.
Code:
#include<stdio.h>
int isprime(int n) //Checks whether the number is prime or not
{
int i;
if(n==1)
return(0);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
{
return(0);
break;
}
}
return(1);
}
int main(void)
{
int count=0,z=0;
int i;
int n;
int x=1;
int reverse2=0;
int z1=0;
int p;
int count1=0;
int digit;
int k=1000000;
int reverse=0;
for(i=2;i<k;i++)
{
if(isprime(i)==1)
{
n=i;
p=i;
while(n>0) // This function removes the digits of the prime number from the right
{
n=n/10;
if(isprime(n)==1)
{
count++;
}
z++;
}
if(z==count)
{
while(p>0) //Checks whether number is left truncatable
{
digit=p%10;
p=p/10;
if(z1==0)
{
reverse=digit;//here reverse doesn't refer to reversing the number. It builds the number one digit at a time from right to left.
}
else
{
reverse=digit*x*10+reverse;
x++;
}
if(isprime(reverse)==1)
{
count1++;
}
z1++;
}
if(z1==count1)
printf("%d ",i);
}
z=0;
z1=0;
count1=0;
count=0;
reverse=0;
reverse2=0;
x=1;
}
}
}
Your left truncatable check is wrong. I did it differently, simpler.
#include<stdio.h>
int isprime(int n) //Checks whether the number is prime or not
{
int i;
if(n==1)
return(0);
for(i=2;i<n/2+1;i++)
{
if(n%i==0)
{
return(0);
break;
}
}
return(1);
}
int power(int a, int b){
int r = 1;
int i=0;
for (i=0;i<b;i++){
r = r * a;
}
return r;
}
int main(void)
{
int count=0,z=0;
int i;
int n;
int z1=0;
int p;
int count1=0;
int digits;
int k=1000000;
for(i=2;i<k;i++)
{
if(isprime(i)==1)
{
z = 0;
count = 0;
n=i;
p=i;
while(n>0) // This function removes the digits of the prime number from the right
{
n=n/10;
if(isprime(n)==1)
{
count++;
}else{
count = -1;
break;
}
z++;
}
if(z==count)
{
z1= 0;
count1=0;
n = i;
p= i;
while(p>0) //Checks whether number is left truncatable
{
digits=n%power(10,z1+1);
p = p /10;
if (isprime(digits)==1)
{
count1++;
}else{
count1 =-1;
break;
}
z1++;
}
if(z1==count1)
printf("%d\n ",i);
}
}
}
}

Resources