Problem 1: Ramanujan Numbers and The Taxicab Problem

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

PROBLEM 1:

RAMANUJAN NUMBERS AND THE TAXICAB PROBLEM

If you mention the number “1729” or the phrase “Taxicab Problem” to any
mathematician, it will immediately bring up the subject of the self-taught Indian
mathematical genius Srinivasa Ramanujan. When Ramanujan was dying of tuberculosis
in a hospital, G. H. Hardy would frequently visit him. It was on one of these visits that the
following occurred according to C. P. Snow.
“Hardy used to visit him, as he lay dying in hospital at Putney. It was on one of
those visits that there happened the incident of the taxicab number. Hardy had
gone out to Putney by taxi, as usual his chosen method of conveyance. He went
into the room where Ramanujan was lying. Hardy, always inept about introducing
a conversation, said, probably without a greeting, and certainly as his first remark:
‘I thought the number of my taxicab was 1729. It seemed to me rather a dull
number.’ To which Ramanujan replied: ‘No, Hardy! No, Hardy! It is a very
interesting number. It is the smallest number expressible as the sum of two cubes
in two different ways.’”

Since then, integer solutions to:


I^3 + J^3 = K^3 + L^3
have been called “Ramanujan Numbers”.

The first 2 of these are:


 

1729 (1^3 + 12^3, 9^3 + 10^3)

4104 (2^3 + 16^3, 9^3 + 15^3)

Given a number, write a program to find whether it is a Ramanujam Number or not.

Input and Output Format:


Input consists of a single integer.

Output yes if it is a Ramanujam Number. Output no otherwise.

 Sample Input 1:

1729

Sample Output 1:
yes

 Sample Input 2:

1854
Sample Output 2:
no

Solution
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,flag=0;;
scanf("%d",&n);
for(i=1;i<n/100;i++)
{
for(j=1;j<n/100;j++)
{
if(pow(i,3)+pow(j,3) == n)
{
printf("yes");
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==0)
printf("no");

return 0;
}
Problem 2:

Write a program to print the following pattern

54321

23456

76543

45678

98765

Input and Output Format:

Input consists of a single integer that corresponds to n, the number of rows in the
pattern.
Refer sample output for output formatting specifications.

Sample Input 1:
5

Sample Output 1:
54321

23456

76543

45678

98765

Sample Input 2:
3

Sample Output 2:
321
234
543
Solution :

#include<stdio.h>
int main()
{
int i,j,val,val1,n;
scanf("%d",&n);
val=n;
for(i=1;i<=n;i++)
{
for(j=0; j<n;j++)
{
printf("%d",val);
val1=val;
if(i%2==0)
val=val+1;
else
val=val-1;
}
printf("\n");
val=val1+1;
}
return 0;
}
Problem 3 :

FIND THE NUMBER WHICH CONTAIN THE DIGIT D

Given two integer numbers n and d. The task is to find the number between 0 to n which
contain the specific digit d.
 

Input format:
Input consists of two integers corresponds to n and d. Assume that the value of d ranges
from 0 < = d < = 9.
 
Output format:
Output corresponds to the numbers between 0 to n, which contains the specific digit d.
Each number is separated by a single whitespace.
 

Sample Input:
20  // n
5    // d
 

Sample Output:
5 15

Solution :

#include<stdio.h>
02 int digit(int x,int d)
03 {
04 while(x>0)
05 {
06 if(x%10==d)
07 break;
08 x=x/10;
09 }
10 return (x>0);
11 }
12 void print(int n,int d)
13 {
14 int i;
15 for(i=0;i<=n;i++)
16 {
17 if(i==d||digit(i,d))
18 printf("%d ",i);
19 }
20 }
21 int main()
22 {
23 int n,d;  
24 scanf("%d\n%d",&n,&d);
25 print(n,d);
26 return 0;
27 }

PROBLEM 4:

COUNT WAYS TO EXPRESS EVEN NUMBER ‘N’ AS SUM OF EVEN INTEGERS

Given an positive even integer ‘n’, write a program to count total number of ways to
express ‘n’ as sum of even positive integers.

Input format:
Input consists of an integer n.

Output format:
Output consists of a number ways to express 'n' as sum of even positive integers.

Sample Input:
6

Sample Output:
4

Explanation
There are only four ways to write 6 as sum of even integers:
1) 2 + 2 + 2
2) 2 + 4
3) 4 + 2
4) 6
 

#include<stdio.h>
#include<math.h>
int main()
{
int n,k,l;
scanf("%d",&n);
k=(n/2)-1;
l=pow(2,k);
printf("%d",l);
return 0;
}

PROBLEM 5:

CALCULATING MATURITY VALUE

Manish opens a Recurring Deposit Account with the Bank of Rajasthan and deposits
Rs.X per month for Y months. Calculate the maturity value of this account, if the bank
pays interest at the rate of Z% per annum.
 

Hint:
Formula for calculating SI is 

Input Format :
First line of input corresponds to principal
Second line of input corresponds to time
Third line of input corresponds to rate of interest
Output Format:
Output corresponds to the maturity value
 

Sample Input 1:
600
20
10
Sample Output 1:
13050
 

Explanation:
By the formula, Manish will get Rs.1050 as simple interest. He also deposits Rs.600 for
20 months, so totally he deposits Rs. 12000 (600*20). 
The total amount Manish will get at the time of maturity is Rs. 13050 (12000+1050)

#include<stdio.h>
int main()
{
float p,n,r,s,t;
scanf("%f %f %f",&p,&n,&r);
s=((p*n*(n+1)*r)/(2*12*100));
t=p*n;
printf("%.f",s+t);
return 0;
}

PROBLEM 6:

TRANSACTION GAIN

A person borrows Rs.Z for 'm' years at X% p.a. simple interest. He immediately lends it
to another person at Y% p.a for the same 'm' years. Find his gain in the transaction per
year.
 

Input format:
First line of input corresponds to the borrowed amount (Rs. Z)
Second line of input corresponds to the interest rate (X%) that he borrowed
Third line of input corresponds to the interest rate (Y%) that he lends it to the another
person
Fourth line of input corresponds to number of years 'm'
Output Format:
Output corresponds to the gain in the transaction per year
 

Sample Input 1:
5000
4
6
2
Sample Output 1:
100

Solution :
#include<stdio.h>
int main()
{
float z,x,y,t;
int n;
scanf("%f %f %f %d",&z,&x,&y,&n);
t=z*(y-x)/100;
printf("%.f",t);
return 0;
}

PROBLEM 6:
 
Write a program to find the centroid of an object in a 2-D grid.
 
A 2D grid is represented by values '0'(zeros) for background and by values '1'(ones)
representing the object. Assume that the coordinates of the 1st point in the grid is (0,0).
Assume that the 2D grid always consists of only one object surrounded by zeros as given
below.

The centroid is a float value pair (xc,yc) where xc is the average of all x coordinate values of
all the coordinates belonging to the object and yc is the average of all y coordinate values of
all the coordinates belonging to the object.
 
Input and Output Format:
 
Input consists of (m*n) + 2 integers.
 
The 1st 2 integers are on 2 separate lines.
The 1st integer corresponds to m, the number of rows in the grid.
The 2nd integer corresponds to n, the number of columns in the grid.
The next 'n' integers correspond to the values in the 1st row.
The next 'n' integers correspond to the values in the 2nd row and so on.
 
Output consists of 2 float values that correspond to the x and y coordinate of the centroid.
The 2 float values are separated by a space. The float values are printed correct to 2 decimal
places.
 
Sample Input:
8
8
00000000
00000000
00111100
00111100
00011000
00011000
00000000
00000000
 
Sample Output:
3.16,3.50

Solution :
i=int(input())
j=int(input())
y=[]
t=[]
r=[]
c=0
for a in range (i):
r.append((input().split()))
#print(r)
for p in range(i):
for h in range(j):
#print(r[p][h])
#print(p,h)
if(r[p][h]=="1"):
y.append(h)
t.append(p)
c=c+1
#print(y,t)
u=round(float(sum(t)/c),2)
s=round(float(sum(y)/c),2)
print("%.2f"% u,end="")
print(",",end="")
print("%.2f"% s,end="")

PROBLEM 7:

Chess Board 1

 
Given the position of a bishop and a queen in a n*n chessboard, mark the remaining
positions in the chess board as follows:

'*' --- if it is under attack from bishop

'$' --- if it is under attack from queen

'%' --- if it is under attack from both queen and bishop.

'.' --- if it is not under attack.


 
 

 
 

Input and Output Format:


Input consists of 5 integers where first integer, n, corresponds to the size of
the chess board.
Second and third integers correspond to the x and y coordinates of the
bishop respectively, and fourth and fifth integers correspond to the x and y
coordinates of the queen respectively.
 

Output consists of a nxn matrix obtained by applying the above rules.


 

Sample Input:
4

4
 

Sample Output:
 

*$*$

.B$$

%$%Q

..$%

Solution :
#include<stdio.h>
int main()
{
int n,x1,y1,x2,y2,i,j,k,h,b[100][100];
char a[100][100];
scanf("%d\n%d\n%d\n%d\n%d",&n,&x1,&y1,&x2,&y2);
a[x1][y1]='B';
a[x2][y2]='Q';
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
b[i][j]=0;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]=='B')
{
h=i;
k=j;
while(h<=n || k<=n)
{
b[h++][k++]+=1;
}
h=i;
k=j;
while(h>=1||k>=1)
{
b[h--][k--]+=1;
}
h=i;
k=j;
while(h>=1||k<=n)
{
b[h--][k++]+=1;
}
h=i;
k=j;
while(h<=n||k>=1)
{
b[h++][k--]+=1;
}
}
else if(a[i][j]=='Q')
{
h=i;
k=j;
while(k<=n)
{
b[h][k++]+=2;
}
h=i;
k=j;
while(h<=n)
{
b[h++][k]+=2;
}
h=i;
k=j;
while(k>=1)
{
b[h][k--]+=2;
}
h=i;
k=j;
while(h>=1)
{
b[h--][k]+=2;
}
h=i;
k=j;
while(h<=n||k<=n)
{
b[h++][k++]+=2;
}
h=i;k=j;
while(h>=1||k>=1)
{
b[h--][k--]+=2;
}
h=i;
k=j;
while(h>=1||k<=n)
{
b[h--][k++]+=2;
}
h=i;
k=j;
while(h<=n||k>=1)
{
b[h++][k--]+=2;
}
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]=='B')
printf("B");
else if(a[i][j]=='Q')
printf("Q");
else
{
if(b[i][j]==0)
printf(".");
else if(b[i][j]==1)
printf("*");
else if(b[i][j]==2)
printf("$");
else if(b[i][j]==3)
printf("%%");

}
printf("\n");
}
return 0;
}

PROBLEM 8:

COIN GAME

There are two players P1 and P2 and two piles of coins consisting of M and N coins respectively. At
each turn, a player can choose only one of the piles out of these and discard the other one. This
discarded pile cannot be used further in the game. The choosed pile is further divided by the player
into two piles of non zero parts. The player who cannot divide the pile i.e. the number of coins in the
pile is < 2, loses the game. The task is to determine which player wins if P1 starts the game and both
the players play optimally.

Examples:

Input: M = 4, N = 4
Output: Player 1
Player 1 can choose any one of the piles as both contain the same number of coins and then splits
the chosen one (the one which is not chosen is discarded) into two piles with 1 and 3 coins
respectively.
Player 2 can choose 3 coins pile and then splits the chosen one into two piles with 1 and 2 coins
respectively.
Player 1 can choose 2 coins pile and then splits the chosen one into two piles with 1 coin each.
Now, player 2 is left with no move (as both the remaining piles contain a single coin each which
cannot be split into two groups of non-zero coins).

Input: M = 1, N = 1
Output: Player 2
There’s no move to make.

Simply check if any of the pile consists of even number of coins. If yes then Player 1 wins else Player
2 wins.
Sample Input 1:
4 (M)
4 (N)

Sample Output 1:
P1

Sample Input 2:
1
1

Sample Output 2:
P2

Solution :

#include<stdio.h>
int main()
{
int m,n;
scanf("%d %d",&m,&n);
if(m%2==0||n%2==0)
printf("P1");
else
printf("P2");
return 0;
}

PROBLEM 9:

COUNT THE NUMBERS

Given two integers X and Y and an array of N integers. Player A can decrease any
element of the array by X and Player B can increase any element of the array by Y. The
task is to count the number of elements that A can reduce to 0 or less. They both play
optimally for infinite time with A making the first move.
Note: A number once reduced to zero or less cannot be increased.
Examples:
Input: a[] = {1, 2, 4, 2, 3}, X = 3, Y = 3
Output: 2

One of the way:


A reduces 2 to -1
B increases 1 to 4
A reduces 2 to -1
B increases 3 to 6 and the game goes on.
Input: a[] = {1, 2, 4, 2, 3}, X = 3, Y = 2
Output: 5
Explanation:
Since the game goes on for infinite time, we print N if X > Y.

Now we need to solve for X ≤ Y. The numbers can be of two types:

Type 1: Those which do not exceed X on adding Y say count1 which can be reduced to
≤ 0 by A.

Type 2:Those which are < X and exceed X on adding Y say count2 only half of which
can be reduced to ≤ 0 by A as they are playing optimally and B will try to increase any
one of those number so that it becomes > X in each one of his turns.
So, the answer will be count1 + ((count2 + 1) / 2).
Input format:
First line of input consists of an integer n, corresponds to the size of array.

The next n lines of input corresponds to the elements in an array.

The next lines consists of an integer number X

The next lines consists of an integer number Y

Output format:
Output consists of an integer which corresponds to the count  of elements 0 or less.

Sample Input :
5 (size of the array)
1
2
3
4
5
3 (X)
3 (y)
Sample Output :
2
Solution :
#include<stdio.h>
int main()
{
int n,a[100],x,y,c1=0,c2=0,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d%d",&x,&y);
if(x>y)
printf("%d",n);
else
{
for(i=0;i<n;i++)
{
if(a[i]+y<=x)
c1++;
}
for(i=0;i<n;i++)
{
if(a[i]<=x&&a[i]+y>x)
c2++;
}
printf("%d",c1+((c2+1)/2));
}
return 0;
}

PROBLEM 10:

ABSOLUTE DIFFERENCE OF SUM

Predict the winner of the game on the basis of absolute difference of sum by selecting
numbers
Given an array of N numbers. Two players X and Y play a game where at every step
one player selects a number. One number can be selected only once. After all the
numbers have been selected, player X wins if the absolute difference between the sum
of numbers collected by X and Y is divisible by 4, else Y wins.
Note: Player X starts the game and numbers are selected optimally at every step.
Examples:
Input: a[] = {4, 8, 12, 16}
Output: X

One of the way:


X chooses 4
Y chooses 12
X chooses 8
Y chooses 16
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 which is divisible by 4.
Hence, X wins
Input: a[] = {7, 9, 1}
Output: Y
Explanation:
The following steps can be followed to solve the problem:

Initialize count0, count1, count2 and count3 to 0.


Iterate for every number in the array and increase the above counters accordingly if a[i]
% 4 == 0, a[i] % 4 == 1, a[i] % 4 == 2 or a[i] % 4 == 3.
If count0, count1, count2 and count3 are all even numbers then X wins else Y will win.
Sample Input 1:
4 (Size of array)
4
8
12
16
Sample Output 1:
X
Sample Input 2:
3 (Size of array)
7
9
1
Sample Output 2:
Y

Solution :

#include<stdio.h>
int main()
{
int a[100],i,n,b1=0,b2=0,b3=0,b4=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%4==0)
b1++;
if(a[i]%4==1)
b2++;
if(a[i]%4==2)
b3++;
if(a[i]%4==3)
b4++;
}
if(b1%2==0&&b2%2==0&&b3%2==0&&b4%2==0)
printf("X");
else
printf("Y");
return 0;
}

PROBLEM 11:

NUMBER OF WAYS FOR PLAYING FIRST MOVE

Two players A and B are playing NIM Game with each other. Both are playing optimally.
Player A starts the game. The task is to find the number of ways of playing 1  move
st

for A to ensure a winning strategy for A if possible, otherwise print -1.


NIM Game rules:
Given a number of piles in which each pile contains some numbers of stones/coins. In
each turn, a player can choose only one pile and remove any number of stones (at least
one) from that pile. The player who cannot move is considered to lose the game (i.e.,
one who take the last stone is the winner). 
Examples:
Input: arr[] = {1, 2, 3}
Output: -1
There is no winning strategy for A no matter how optimally he plays.
Input: arr[] = {2, 4, 5}
Output: 1
Explanation:
In order to play optimally, A will pick one coin from first pile and that’s the only optimal
move.

 First check who will win the game by taking XOR of all the array elements if XOR
is zero then no matter how optimally A play, A will always lose. If XOR is non-zero then
go to Step 2.
 We will check for every pile if we can remove some coins from that pile so that
after this move, XOR of all the array elements will be zero. So for all piles, one by one
we will take xor of all remaining elements of the array and will check if XOR value is
greater than the number of coins in the pile. If so, it is not possible to play the first move
using this pile because we can only remove coins from the pile in a move, and cannot
add coins. Otherwise, we will increment the number of ways.
Sample Input 1:
3 (size of array)
1
2
3
Sample Output 1:
-1
Sample Input 2:
3 (size of array)
2
4
5
Sample Output 2:
1

Solution :
#include<stdio.h>
int main()
{
int b,i,n,s=0,t,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
s=a[0];
for(i=1;i<n;i++)
{
s=s^a[i];
}
t=s;
if(t==0)
printf("-1");
else
{
b=0;
for(i=0;i<n;i++)
{
if((t^a[i])<a[i])
b++;
}
printf("%d",b);
}
return 0;
}

You might also like