** RUN THE PROGRAM AGAIN IF U GET INTERNAL ERROR **
PROGRAMMING
PROBLEM 1
Problem Statement
A prime number is a natural number greater than 1 and has exactly 2 divisors which are 1 and the
number itself.
You are given a prime number n, find any 2 prime numbers a and b such that a+b=n or state that no
such pair of primes exists.
Input Format
The input contains a single prime number n.
Constraints
(2≤n≤10^7)
Output Format
If there doesn't exist any 2 primes such that their summation is equal to n then print -1, otherwise print
the 2 primes on a single line separated by a space the smaller number should preceed the larger
number.
SOLUTION
C++ CODE
#include <bits/stdc++.h>
using namespace std;
bool isp(int x){
if (x < 2)return false;
for (int i = 2; i * i <= x; ++i)if (x % i == 0)return false;
return true;
}
int main(){
int x;
scanf("%d", &x);
if (isp(x - 2))printf("%d %d\n", 2, x - 2);
else printf("-1\n");
}
PROBLEM 2
Problem Statement
Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even
integer greater than 2 can be expressed as the sum of two primes. That got Nick's attention and he
decided to invent a problem of his own and call it Noldbach problem. Since Nick is interested only in
prime numbers, Noldbach problem states that at least k prime numbers from 2 to n inclusively can be
expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19
= 7 + 11 + 1, or 13 = 5 + 7 + 1.
Two prime numbers are called neighboring if there are no other prime numbers between them.
You are to help Nick, and find out if he is right or wrong.
Input Format
The first line of the input contains two integers n and k.
Constraints
(2 ≤ n ≤ 1000)
(0 ≤ k ≤ 1000)
Output Format
Print "YES"(without subquotes) if at least k prime numbers from 2 to n inclusively can be expressed as
it was described above. Otherwise output NO.
SOLUTION :
PYTHON CODE :
n,k=map(int,input().strip().split())
v = []
for i in range(2,n+1):
if all(i%j!=0 for j in v):
v.append(i)
c=0
for i in range(len(v)-1):
if v[i]+v[i+1]+1 in v:
c+=1
if(c>=k):
print("YES")
else:
print("NO")
PROBLEM 3
Problem Statement
For a given number N check if it is prime or not. A prime number is a number which is only divisible by 1
and itself.
Input Format
A single line of input contains an integer N.
Constraints
1 <= N <= 10^9
Output Format
Print 1 if N is a prime number or 0 otherwise
SOLUTION :
C CODE :
#include <math.h>
#include <stdio.h>
int main()
{
int n, i, flag = 1;
scanf("%d", &n);
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
flag = 0;
break;
}
}
if (n <= 1)
flag = 0;
if (flag == 1) {
printf("1");
}
else {
printf("0");
}
return 0;
}
*** RUN THE PROGRAM MULTIPLE TIMES UNTIL U GET ALL THE OUTPUTS **
Post a Comment