Change language

Check if the sum of the absolute difference of adjacent digits is prime or not

|

Examples :

  Input:  N = 142  Output:  Prime Sum = | 1-4 | + | 4-2 | = 5 ie prime.  Input:  N = 347  Output:  Not prime 

Approach : find the sum of the absolute difference of adjacent digits and then check if whether this amount is

// C++ implementation of the above approach
# include "bits / stdC++. h"

 

using namespace std;

 

 
// Function for checking 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 for finding the sum of an 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" ;

else

  cout "" " Not Prime " ;

 

return 0;

 
}

Java

// Java implementation of the above approach

import java.util. *;

 

class GFG 

{

  

// Function for checking 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 for finding the sum of an 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 provided
// sanjeev2552

python3

# Python3 implementation of the above approach

import math as mt

 
# Function for checking 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 for finding the sum of an 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 " )

 
# This code is provided by Mohit Kumar

C #

// C # implementation of the above approach

using System;

 

class GFG 

{

  

// Function for checking 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 for finding the sum of an 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 provided by Rajput-Ji

Exit:

 Prime 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically