Given a number N, the task is to check if its first and last digit is a prime number and their sum is less than K, or not. If it is, then print Yes, else print No.
Examples:
Input: N = 322223, K = 10
Output: Yes
Input: N = 62531561, K = 15
Output: No
Approach:
- Check if the first and last digit of N is prime or not.
- If they are prime, then check if they are less than K or not
- If they are less than K as well, print Yes.
- In any other case, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int first( int n)
{
int a = n;
int c = 1;
while (a != 0)
{
a /= 10;
c = c * 10;
}
c = c / 10;
int fi = n / c;
return fi;
}
bool prime( int n)
{
switch (n)
{
case 2:
return true ;
case 3:
return true ;
case 5:
return true ;
case 7:
return true ;
default :
return false ;
}
}
void check( int n, int k)
{
int l = n % 10;
int f = first(n);
bool lp = prime(l);
bool fp = prime(f);
if (lp && fp)
{
if (l + f < k)
cout << "Yes\n" ;
else
cout << "No\n" ;
}
else
{
cout << "No\n" ;
}
}
int main()
{
int n = 322223;
int k = 10;
check(n, k);
n = 62531561;
k = 15;
check(n, k);
return 0;
}
|
Java
import java.lang.*;
public class crazy_number {
public static int first( int n)
{
int a = n;
int c = 1 ;
while (a != 0 ) {
a /= 10 ;
c = c * 10 ;
}
c = c / 10 ;
int fi = n / c;
return fi;
}
public static boolean prime( int n)
{
switch (n) {
case 2 :
return true ;
case 3 :
return true ;
case 5 :
return true ;
case 7 :
return true ;
default :
return false ;
}
}
public static void check( int n, int k)
{
int l = n % 10 ;
int f = first(n);
boolean lp = prime(l);
boolean fp = prime(f);
if (lp && fp) {
if (l + f < k)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
else {
System.out.println( "No" );
}
}
public static void main(String args[])
{
int n = 322223 ;
int k = 10 ;
check(n, k);
n = 62531561 ;
k = 15 ;
check(n, k);
}
}
|
Python3
def first(n):
a = n
c = 1
while (a ! = 0 ):
a / / = 10
c = c * 10
c = c / / 10
fi = n / / c
return fi
def prime(n):
if n in [ 2 , 3 , 5 , 7 ]:
return True
else :
return False
def check(n, k):
l = n % 10
f = first(n)
lp = prime(l)
fp = prime(f)
if (lp and fp):
if (l + f < k):
print ( "Yes" )
else :
print ( "No" )
else :
print ( "No" )
n = 322223
k = 10
check(n, k)
n = 62531561
k = 15
check(n, k)
|
C#
using System;
class GFG
{
public static int first( int n)
{
int a = n;
int c = 1;
while (a != 0)
{
a /= 10;
c = c * 10;
}
c = c / 10;
int fi = n / c;
return fi;
}
public static Boolean prime( int n)
{
switch (n)
{
case 2:
return true ;
case 3:
return true ;
case 5:
return true ;
case 7:
return true ;
default :
return false ;
}
}
public static void check( int n, int k)
{
int l = n % 10;
int f = first(n);
Boolean lp = prime(l);
Boolean fp = prime(f);
if (lp && fp)
{
if (l + f < k)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String []args)
{
int n = 322223;
int k = 10;
check(n, k);
n = 62531561;
k = 15;
check(n, k);
}
}
|
Javascript
<script>
function first(n) {
var a = n;
var c = 1;
while (a != 0) {
a = parseInt(a / 10);
c = c * 10;
}
c = c / 10;
var fi = parseInt(n / c);
return fi;
}
function prime(n) {
switch (n) {
case 2:
return true ;
case 3:
return true ;
case 5:
return true ;
case 7:
return true ;
default :
return false ;
}
}
function check(n, k) {
var l = parseInt(n % 10);
var f = first(n);
var lp = prime(l);
var fp = prime(f);
if (lp && fp) {
if (l + f < k)
document.write( "Yes <br>" );
else
document.write( "No <br>" );
} else {
document.write( "No <br>" );
}
}
var n = 322223;
var k = 10;
check(n, k);
n = 62531561;
k = 15;
check(n, k);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1), As constant extra space is used
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Mar, 2023
Like Article
Save Article