Programs
Programs
Easy:
1. You are embarking on a mission to secure a treasure chest with a secret code.
To unlock the chest, you need to find the largest prime factor of a mysterious number.
The code to the chest is derived from this prime factor.
Constraints:
0 <= n <= 5 * 106
Example 1:
Sample Input:
12
Sample Output:
3
Example 2:
Sample Input:
56
Sample Output:
7
*******************************************************************************/
#include <stdio.h>
#include<math.h>
int largePrimeFactor(int num)
{
int i,max=0;
while(num%2==0)
{
max=2;
num=num/2;
}
for(i=3;i<=sqrt(num);i=i+2)
{
while(num%i==0)
{
max=i;
num=num/i;
}
}
if(num>2)
{
max=num;
}
return max;
}
int main()
{
int num;
printf("Sample input:");
scanf("%d",&num);
printf("Sample output:%d",largePrimeFactor(num));
return 0;
}
'''2. You're participating in a math competition, and one of the challenges involves
identifying number palindromes. The competition organizers provide you with a list of numbers
and ask you to determine which of them are palindromes. Your task is to create a Python program
to quickly identify these number palindromes.
Constraints:
-231 <= x <= 231 - 1
Example 1:
Sample Input: 121
Sample Output: Palindrome
Example 2:
Sample Input:12345
Sample Output: Not Palindrome'''
num=int(input("Sample input:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("Sample Output:Palindrome")
else:
print("Sample Output:Not Palindrome")
/******************************************************************************
You are on a secret mission in a foreign country where you've discovered a coded message.
The message is in binary format, and you suspect it holds critical information.
To decode the message, you need to convert the binary numbers to decimal.
Constraints:
0 <= n <= 105
Example 1:
Sample Input: 1101
Sample Output: 13
Example 2:
Sample Input: 10101
Sample Output: 21
*******************************************************************************/
#include <stdio.h>
#include<math.h>
int bintodec(int num)
{
int dec=0,i=0,rem;
while(num!=0)
{
rem=num%10;
dec+=rem*pow(2,i);
num/=10;
i++;
}
return dec;
}
int main()
{
int num;
printf("Sample Input : ");
scanf("%d",&num);
printf("Sample Output : %d",bintodec(num));
return 0;
}
/******************************************************************************
5)
You are an archaeologist exploring an ancient civilization. While deciphering inscriptions
on ancient stone tablets, you come across a legend about a series of perfect numbers believed
to hold mystical powers. These numbers were said to have been used in sacred rituals.
Constraints:
1 <= num <= 105
EXAMPLE 1:
Input
Enter the starting element of the range : 1
Enter the ending element of the range : 10
Output
All Perfect numbers between 1 to 10: 6
EXAMPLE 2:
InputM
Enter the starting element of the range : 1
Enter the ending element of the range : 30
Output
All Perfect numbers between 1 to 30:6
*******************************************************************************/
#include <stdio.h>
int main() {
int startRange, endRange;
int count = 0;
int perfectNumbers[100];
return 0;
}
/******************************************************************************
6.
You are a brilliant young mathematician who has been summoned to a remote island to
help solve an ancient puzzle. The island's inhabitants believe that Fibonacci numbers
hold the key to unlocking a hidden treasure buried deep within a cave.
The problem is that the cave's entrance is sealed by a mysterious lock, and you must
provide the correct sequence of Fibonacci numbers as the code to open it. To do this efficiently,you
decide to use matrix exponentiation, a mathematical technique that can compute Fibonacci numbers
quickly.
Constraints:
0 <= n <= 60
EXAMPLE 1:
Input (n): 0
Output: 0
EXAMPLE 2:
Input (n): 5
Output: 5
*******************************************************************************/
#include <stdio.h>
void multiply(int F[2][2], int M[2][2]) {
int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
multiply(F, M);
}
int fibonacci(int n) {
if (n == 0)
return 0;
int main() {
int n;
printf("Input(n):")
scanf("%d",&n);
int result = fibonacci(n);
printf("Output : %d\n", result);
return 0;
}