Given an integer N
N<=10^50
Output the no. of pairs (x,y) such that
0<=x<=n
0<=y<=n
F(x) +F(y) = Prime number
F(x) = sum of digits of x
Note : (x,y) and (y,x) are to be treated as same pair
Example
Input
3
Output
5
Explanation
5 pairs (0,2), (1,1), (0,3), (2,3), (1,2) give prime no.s
Do you the solution please update
the solution also
a=int(input())
at=[]
hh=0
for i in range(a):
for j in range(a):
hh=i+j
if(hh%2!=0 or hh%3!=0 or hh%5!=0 or hh%7!=0 or hh==2 or hh==3 or hh==5 or hh==7):
ll=[i,j]
ll=sorted(ll)
if (ll) not in at:
at.append(ll)
print(ll)
print(len(at))
Cant you see the constraints pagal jaise kuch v
school ka ques nahi h ye
8 LPA ke liye h
The question says:
“Sum of digits of X + Sum of digits of Y should be a prime number
whereas the test case says:
“Sum of X and Y should be prime number”
thats because the test case is 3.
3 is a single-digit number.
sum of digits of a single digit number is the number itself.
Here is the solution in Python3
from itertools import permutations
#for checking the prime number or primality test
def isprime(no):
isprime=True
if(no==1):
return False
for i in range(2,n//2+1):
if(no%i==0):
isprime=False
return isprime
return isprime
#for getting the sum of digits of any number
def sumofdigit(s):
tot=0
for i in str(s):
tot+=int(i)
return tot
#taking input as no
n=int(input())
#converting to list as range from 0 to n
l=list(range(n+1))
#for making permutations
permute=permutations(l,2)
count_pair=0
for data in permute:
if(data[0]<data[1] and isprime(sumofdigit(data[0])+sumofdigit(data[1]))):
print(data)
count_pair+=1
print(count_pair)
c++ solution
#include
using namespace std;
bool isprime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i >n;
vector<pair> arr;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c=i+j;
if (isprime(c))
arr.emplace_back(i,j);
}
}
for(auto it1: arr)
cout<<it1.first<<it1.second<<" ";
cout<<arr.size();
}
time limit will exceed bro
import java.util.*;
class spprme
{
static int soe(int n)
{
int c=0;
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for(int i=2;i<n+1;i++)
{
if(prime[i]==true)
{
if(i==2)
c+=2;
if(i2)
{
int x=(i/2)+1;
c+=x;
}
if(i>(n/2))
{
int x=(i/2)+1;
int y=x-(i-(n/2));
c+=y;
}
}
}
return c;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
System.out.print(soe(2*s));
}
}