Sum of primes | Practice | GeeksforGeeks

Your task is to calculate sum  of primes present as digits of given number N.
Example 1:
Input: 333
Output: 9
Explaination: 3 is a prime number. It 
is present 3 times. So 3+3+3 = 9.
Example 2:
Input: 686
Output: 0
Explaination: Neither 6 nor 8 is a 
prime number.
Your Task:
You do not need to read input or print anything. Your task is to complete the function primeSum() which takes N as input parameter and returns the sum of all the prime digits present in the number N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 5*104   
0
mayank180919991 week ago
   int primeSum(int N){
        // code here
        int digit,sum=0;
        while(N!=0){
            digit=N%10;
            if(digit==2||digit==3||digit==5||digit==7){
                sum+=digit;
            }
            N=N/10;
        }
     return sum;
    }
0
adityapratyush1 month ago
Python Solution:
 
def primeSum(self, N):       # code here       def isPrime(num):           if num < 2:               return 0           elif num == 2:               return 1           else:               for i in range(2, num):                   if num % i == 0:                       return 0               return 1
       add = 0       for char in str(N):           char = int(char)           s = isPrime(char)           if s == 1:               add += char       return add
0
mayank20212 months ago
int primeSum(int N){              int sum=0;       while(N)       {           if(N%10==2 || N%10==3 || N%10==5 || N%10==7 )            sum=sum+N%10;                      N=N/10;                  }              return sum;                 }
0
mayank20212 months ago
C++int isprime(int N)    {        if(N==1) return 0;        for(int i=2; i*i<=N; i++)        {            if(N%i==0) return 0;        }        return N;    }   int primeSum(int N){              int sum=0;              while(N)       {           sum=sum+isprime(N%10);           N=N/10;       }              return sum;          }
0
singhraunaq112 months ago
bool isPrime(int num){       int flag=1;       if(num==0 || num==1){           return false;       }       for(int i=2;i<num;i++){           if(num%i==0){               flag=0;               break;           }       }       if(flag==1){           return true;       }       else{           return false;       }   }   int primeSum(int num){       int rem=0,rev=0,sum=0;       while(num!=0){           rem=num%10;           num=num/10;           if(isPrime(rem)){               sum=sum+rem;           }       }       return sum;   }
//Time taken: 0.0s
+2
shubhamkhavare3 months ago
Easy java soln:
class Solution{   static Boolean isPrime(int n){       if(n <= 1)       {           return false;       }       for(int i = 2 ; i <= Math.sqrt(n); i++)       {           if(n%i == 0)           {               return false;           }       }       return true;   }   static int primeSum(int N){       int sum = 0;       while(N != 0)       {           int m = N%10;           if(isPrime(m))           {               sum += m;           }           N = N/10;       }       return sum;   }}
0
devil043 months ago
java solution 0.1/1.6
 
 int i,j,c=0,s=0;       while(N>0)       {           i=N%10;           for(j=1;j<=i;j++)           {               if(i%j==0)c++;           }           if(c==2)           {           s=s+i;           }           c=0;           N=N/10;       }       return s;   }
0
8murugans3 months ago
class Solution{public:   int primeSum(int N){   int d,s=0;   while(N>0)   {       d = N%10;       N = N/10;       if(d==2 || d==3 || d==5 || d==7)       {           s+=d;       }       else       {           s+=0;       }   }   return s;   }};
+1
showpnilkumar211020013 months ago
class Solution{public:   int primeSum(int N){       // code here       int sum=0;       while(N!=0)       {           int rem=N%10;           if(rem==2||rem==3||rem==5||rem==7)           {               sum+=rem;           }           N=N/10;       }       return sum;   }
0
thatsiddharth3 months ago
// Initial Template for Java
import java.io.*;import java.util.*;
class GFG{   public static void main(String args[])throws IOException   {       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));       int t = Integer.parseInt(in.readLine());       while(t-- > 0){           int N = Integer.parseInt(in.readLine());                      Solution ob = new Solution();           System.out.println(ob.primeSum(N));       }   }}// } Driver Code Ends
// User function Template for Java
class Solution{   static int primeSum(int N){       // code here      int i,j,k=0,sum=0,p; int n=N;      while(N!=0)      {          p=N%10;                    for(j=1;j<=p;j++)              {                  if(p%j==0)                  k++;              }              if(k==2)              sum+=p;              k=0;                    N/=10;      }      return sum;  }     }
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.