Skip to content
Related Articles

Related Articles

Check whether the sum of absolute difference of adjacent digits is Prime or not
  • Last Updated : 13 Apr, 2021

Given a number a N and the task is to check weather the sum of absolute difference of adjacent digit is a prime or not.

Examples: 

Input: N = 142 
Output: Prime
Sum = |1-4| + |4-2| = 5 i.e. prime.

Input: N = 347
Output: Not prime 

Approach: Find the sum of absolute difference of adjacent digits and then check if that sum is prime or not. 
Below is the implementation of the above approach:  

C++




// C++ implementation of the above approach
#include<bits/stdc++.h>
 
using namespace std;
 
 
// Function to check for a prime number
bool Prime(int n){
 
    if( n == 1){
        return false;
            }
    for (int i=2;i*i<=n;i++){
        if (n % i == 0)
            return false;
                    }
    return true;
}
 
// Function to find the sum of array
bool checkSumPrime(string st){
    int summ = 0;
    for (int i=1;i<st.size();i++)
        summ+= abs(st[i-1]-st[i]);
 
    if(Prime(summ))
        return true;
    else
        return false;
}
 
// Driver code
int main(){
 
    int num = 142;
 
    string s= "142";
 
    if (checkSumPrime(s))
        cout<<"Prime\n";
    else
        cout<<"Not Prime\n";
 
return 0;
 
}

Java




// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
    // Function to check for a prime number
    static boolean Prime(int n)
    {
        if (n == 1)
            return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return false;
        return true;
    }
 
    // Function to find the sum of array
    static boolean checkSumPrime(String str)
    {
        int summ = 0;
        for (int i = 1; i < str.length(); i++)
            summ += Math.abs(str.charAt(i - 1) -
                             str.charAt(i));
 
        if (Prime(summ))
            return true;
        else
            return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int num = 142;
        String str = "142";
        if (checkSumPrime(str))
            System.out.println("Prime");
        else
            System.out.println("Not Prime");
    }
}
 
// This code is contributed by
// sanjeev2552

Python3




# Python3 implementation of the above approach
import math as mt
 
# Function to check for a prime number
def Prime(n):
     
    if n == 1:
        return False
         
    for i in range(2, mt.ceil(mt.sqrt(n + 1))):
        if n % i == 0:
            return False
    return True
     
# Function to find the sum of array
def checkSumPrime(string):
    summ = 0
    for i in range(1, len(string)):
        summ += abs(int(string[i - 1]) -
                    int(string[i]))
         
    if Prime(summ):
        return True
    else:
        return False
 
# Driver code
num = 142
 
string = str(num)
 
s = [i for i in string]
 
if checkSumPrime(s):
    print("Prime")
else:
    print("Not Prime\n")
 
# This code is contributed by Mohit Kumar

C#




// C# implementation of the above approach
using System;
     
class GFG
{
 
    // Function to check for a prime number
    static bool Prime(int n)
    {
        if (n == 1)
            return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return false;
        return true;
    }
 
    // Function to find the sum of array
    static bool checkSumPrime(String str)
    {
        int summ = 0;
        for (int i = 1; i < str.Length; i++)
            summ += Math.Abs(str[i - 1] -
                             str[i]);
 
        if (Prime(summ))
            return true;
        else
            return false;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "142";
        if (checkSumPrime(str))
            Console.WriteLine("Prime");
        else
            Console.WriteLine("Not Prime");
    }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function to check for a prime number
function Prime(n)
{
    if (n == 1)
        return false;
         
    for(let i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
             
    return true;
}
 
// Function to find the sum of array
function checkSumPrime(str)
{
    let summ = 0;
    for(let i = 1; i < str.length; i++)
        summ += Math.abs(str[i - 1]-
                         str[i]);
 
    if (Prime(summ))
        return true;
    else
        return false;
}
 
// Driver Code
let num = 142;
let str = "142";
 
if (checkSumPrime(str))
    document.write("Prime");
else
    document.write("Not Prime");
     
// This code is contributed by unknown2108
 
</script>
Output: 
Prime

 

Time Complexity: O(sum1/2), where the sum is the sum of the digits of the number

Auxiliary Space: O(1)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :