Nothing Special   »   [go: up one dir, main page]

Codevita 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Problem : Consecutive Prime Sum

Some prime numbers can be expressed as Sum of other consecutive

prime numbers.

For example

5=2+3

17 = 2 + 3 + 5 + 7

41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this

property are present in the range 3 to N subject to a constraint that

summation should always start with number 2.

Write code to find out number of prime numbers that satisfy the above

mentioned property in a given range.

Input Format:

First line contains a number N

Output Format:

Print the total number of all such prime numbers which are less than

or equal to N.

Sample Input and Output:

SNo.InputOutputComment

(Below 20, there are 2 such numbers: 5 and 17).


1 20 2
5=2+3
17=2+3+5+7
2 15 1
C:-
#include<stdio.h>
main()
{
int N,c=0,sum=2,a,b,i,j,k;
scanf("%d",&N);
for(i=3;i<=N;i++)
{
a=1,b=1;
for(j=2;j<=i/2;j++)
if(i%j==0)
{
a=0;
break;
}
if(a==1)
{
sum+=i;
if(sum<=N)
{
for(k=2;k<=sum/2;k++)
if(sum%k==0)
{
b=0;
break;
}
if(b==1)
c++;
}
}
}
printf("%d\n",c);
}

Problem : Fibonacci Right Triangles

The Fibonacci series 1,1,2,3,5,8,... is well known. In this series, if F n is the nth number

F1=1, F2=1, Fn=Fn-1+Fn-2


Every alternate number in the series stating with 5 (5,13,34,...) can be the hypotenuse of a right angled
triangle. We call each of these a Fibonacci Right Angled Triangle, or FRAT for short. The larger of the
remaining two sides is the nearest integer to (2/sqrt(5)) times the hypotenuse.

A factor of a positive integer is a positive integer with divides it completely without leaving a remainder. For
example, for the number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two
factors, 1 and the number k itself.

The objective of the program is to find the number of factors of the even side of the smallest FRAT whose
hypotenuse is odd and is larger than a given number

Input

The input is a positive integer N

Output

The number of factors of the even side of the smallest FRAT which has an odd hypotenuse greater than N

Constraints

The even side is less than 5000000

Example 1

Input:
10

Output:
6

Explanation:
The smallest FRAT that has an hypotenuse greater than 10 is (13,12,5). As the hypotenuse is odd, we take
this. The even side is 12, factors are (1,2,3,4,6,12), a total of 6 factors. The output is 6

Example 2

Input:

20

Output:
10

Explanation:
The smallest FRAT that has an hypotenuse greater than 20 is (34,30,16). As the hypotenuse is even, we
take the next FRAT, which is (89,80,39). The even side is 80, factors are (1,2,4,5,8,10,16,20,40,80), a total
of 10 factors. The output is 10

C:-
#include<stdio.h>
#include<math.h>
int fibonacci(int);
int r1(float);
int facts(int);
int main() {
int n,i,j,l;
float k;
scanf("%d",&n);
i=fibonacci(n);//to find the next odd number of n in fib series
k=2/sqrt(5)*i;
j=k+r1(k); //to find the side of the triangle nearest to i
l=sqrt(pow(i,2)-pow(j,2)); //to find another side of the triangle
if(j%2==0)
printf("%d",facts(j));
else if(l%2==0)
printf("%d",facts(l));
return 0;
}
//to find the no. of factors
int facts(int num) {
int i,c=0;
for(i=1;i<=num;i++) {
if(num%i==0)
c++;
}
return c;
}

int r1(float i) { //for rounding the number i


int m=i;
float x=i-m;
if(x>0.5)
return 1;
else
return 0;
}
int fibonacci(int n){
int a=5,b=8,c;
while(1){
c=b+a;
if(a>5 && a>n && a%2!=0) {
return a;
}
a=b;
b=c;
c=b+a;
a=b;
b=c;
}
}

Problem : A robot is programmed to move forward F meters and backwards again, say B
meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is a
ditch at a distance FD from initial position in forward direction as well as a ditch at a distance
BD from initial position in backward direction. This forward and backward movement is
performed repeatedly by the Robot.
Your task is to calculate amount of time taken, before the Robot falls in either ditch, if at all it
falls in a ditch.
Input Format: First line contains total number of test cases, denoted by N Next N lines, contain
a tuple containing 5 values delimited by space F B T FD BD, where
1. F denotes forward displacement in meters
2. B denotes backward displacement in meters
3. T denotes time taken to cover 1 meter
4. FD denotes distance from Robot's starting position and the ditch in forward direction
5. BD denotes distance from Robot's starting position and the ditch in backward direction
Output Format: For each test case print time taken by the Robot to fall in the ditch and also
state which ditch he falls into. Print F for forward and B for backward. Both the outputs must be
delimited by whitespace OR Print No Ditch if the Robot does not fall in either ditch.
Constraints:
First move will always be in forward direction
1 <= N <= 100
forward displacement > 0
backward displacement > 0
time > 0
distance of ditch in forward direction (FD) > 0
distance of ditch in backward direction (BD) > 0
All input values must be positive integers only
Sample Input and Output
SNo.
Input
Output
1
3 9 4 3 13 10 9 7 1 11 13 4 4 3 8 12
63 F 25 F No Ditch
2
5 8 4 7 11 22 4 5 4 25 6 4 9 3 6 29 7 10 6 24 12 10 10 1 9 7
133 F 216 B 231 B 408 B No Ditch

C++:-

#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
int f[100],b[100],t[100],fd[100],bd[100],n,time;
cin>>n;//number of test cases
for(int i=0;i<n;i++){
cin>>f[i]>>b[i]>>t[i]>>fd[i]>>bd[i];
/*to read 5 tuple variables that contain forward distance,backward diatance,time taken
to cover 1 meter,forward ditch distance,backward ditch distance*/
}
//to check no ditch condition
for(int i=0;i<n;i++){
int init=0;
int newinit=0;
int time=0;
if(f[i]==b[i]){
cout<<"No Ditch\n";
}
while(1){
//forward ditch
if(init < fd[i]){
time+=t[i]*f[i];
init+=f[i];

if(init > fd[i]){


newinit=init-fd[i];
cout<<time-(newinit*t[i])<<" F\n";
break;
}
else if(init == fd[i]){
newinit=init;
cout<<time<<" F\n";
break;
}
}
//backward ditch
if(init < bd[i]){
time+=t[i]*b[i];
init-=b[i];
if(abs(init) > bd[i]){
newinit=abs(init)-abs(bd[i]);
cout<<time-(newinit*t[i])<<" B\n";
break;
}
else if(abs(init)== bd[i]){
newinit=abs(init);
cout<<time<<" B\n";
break;
}
}
}
}
return 0;
}

Problem : Sheldon Cooper and his beverage paradigm


Sheldon Cooper, Leonard Hofstadter and Penny decide to go for drinks at Cheese cake factory. Sheldon
proposes to make a game out of this. Sheldon proposes as follows,
To decide the amount of beverage they plan to consume, say X.
Then order for a random number of different drinks, say {A, B, C, D, E, F} of quantities {a, b, c, d, e, f}
respectively.
If quantity of any three drinks add up to X then we'll have it else we'll return the order. E.g. If a + d + f =
X then True else False

Input Format:
1. First line contains number of bottles ordered denoted by N
2. Next N lines, contains a positive integer Ai, the size of the ith bottle
3. Last line contains the quantity they intend to consume denoted by X in text above

Your task is to help find out if there can be any combination of three beverage sizes that can sum up to
the quantity they intend to consume. If such a combination is possible print True else False

Output Format: True, if combination is possible False, if combination is not possible

Constraints:
N >= 3
Ai > 0
1 <= i <= N
X>0
Sample Input and Output
SNo.
Input
Output
1
6 1 4 45 6 10 8 22
True
2
4 1 3 12 4 14
False

C:-
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,*a=NULL,x,i,j,k,sum=0,flag=0;
scanf("%d",&n);//number of beverages
a=(int*)calloc(n,sizeof(int));
for(i=1;i<=n;i++){
scanf("%d",&a[i]);//to read quantities of bevarages
}
scanf("%d",&x);//quantity required
for(i=1;i<=n;i++){
if(a[i]>x){
a[i]=a[n];
n--;
i--;
}
}
for(i=1;i<=n;i++){
sum=a[i];
for(j=i+1;sum+a[j]<x&&j<=n;j++) {//to select second element
sum+=a[j];
for(k=j+1;k<=n;k++) { //to check third element
if(sum+a[k]==x){
flag=1;
break;
}
}
sum-=a[j];
}
sum-=a[i];
}
if(flag==1)
printf("true");
else
printf("false");
return 0;
}

You might also like