Codevita 2
Codevita 2
Codevita 2
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
Write code to find out number of prime numbers that satisfy the above
Input Format:
Output Format:
Print the total number of all such prime numbers which are less than
or equal to N.
SNo.InputOutputComment
The Fibonacci series 1,1,2,3,5,8,... is well known. In this series, if F n is the nth number
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
Output
The number of factors of the even side of the smallest FRAT which has an odd hypotenuse greater than N
Constraints
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;
}
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];
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
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;
}