# Find Prime Nodes Sum Count in Non-Binary Tree

### Implementation

``````// Writing a C++ program that will count the nodes which contain the prime digit and hold the sum of the weights in a binary tree

#include <bits/stdc++.h>
using namespace std;

int MAX = 1000000;

Vecc<int> graph;
Vecc<int> weight(100);

// Creating a function that will create the sieve and check whether it is prime or not.
void SieveOfEratosthenes(
bool prime[], int p_size)
{
// If the value we get is false, then it will indicate that the value is not prime
prime = false;
prime = false;

for (int p = 2; p * p <= p_size; p++) {

//If the prime say [p] is not changed, it is a prime number.
if (prime[p]) {

// We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
for (int i = p * 2;
i <= p_size;
i += p)
prime[i] = false;
}
}
}

// Now, we have to create a function that will give us the sum of the digits of the n
int digitSum(int n)
{
int sum = 0;
while (n) {
sum += n % 10;
n = n / 10;
}
return sum;
}

// Now, we have to create a function for the dfs
void dfs(int __nod,
int parent,
bool prime[])
{
//If the sum of the digits turns out to be the weight of the current node, then it is prime, and then we have to increase the answerer.
int sum = digitSum(weight[__nod]);
if (prime[sum])

for (int to : graph[__nod]) {
if (to == parent)
continue;
dfs(to, __nod, prime);
}
}

// writing the main code to test the above functions
int main()
{

// Extracting the weight of the nodes
weight = 144;
weight = 1234;
weight = 21;
weight = 5;
weight = 77;

// Evaluating the edges of the tree
graph.push_back(2);
graph.push_back(3);
graph.push_back(4);
graph.push_back(5);

bool prime[MAX];
memset(prime, true, sizeof(prime));

SieveOfEratosthenes(prime, MAX);

dfs(1, 1, prime);

return 0;
}
``````

Output: Example 2)

``````// Writing a C# program that will count the nodes which contain the prime digit and hold the sum of the weights in a binary tree
using System;
using System.Collections.Generic;
class TPT{

static int MAX = 1000000;
static List<int> []graph =
new List<int>;
static int []weight = new int;

// Creating a function that will create the sieve and check whether it is prime or not.
static void SieveOfEratosthenes(bool []prime,
int p_size)
{
// If the value we get is false, then it will indicate that the value is not prime
prime = false;
prime = false;

for (int p = 2; p * p <= p_size; p++)
{
// In case, the prime say [p] is not changed, then it is a prime number.
if (prime[p])
{
// We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
for (int i = p * 2;
i < p_size; i += p)
prime[i] = false;
}
}
}
// Now, we have to create a function that will give us the sum of the digits of the n
static int digitSum(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
// Now, we have to create a function for the dfs
static void dfs(int __nod,
int parent,
bool []prime)
{
//If the sum of the digits turns out to be the weight of the current node, then it is prime, and then we have to increase the answer.
int sum = digitSum(weight[__nod]);
if (prime[sum])

foreach (int to in graph[__nod])
{
if (to == parent)
continue;
dfs(to, __nod, prime);
}
}
// writing the main code to test the above functions
public static void Main(String[] args)
{
// Extracting the weight of the nodes
weight = 144;
weight = 1234;
weight = 21;
weight = 5;
weight = 77;
for (int i = 0; i < graph.Length; i++)
graph[i] = new List<int>();
// Evaluating the edges of the tree

bool []prime = new bool[MAX];

for (int i = 0; i < prime.Length; i++)
prime[i] = true;

SieveOfEratosthenes(prime, MAX);
dfs(1, 1, prime);
}
}``````

Output: Example 3)

``````// Writing a Java program that will count the nodes which contain the prime digit and hold the sum of the weights in a binary tree
import java.util.*;
class TPT{

static int MAX = 1000000;

static Vecc<Integer> []graph =
new Vecc;
static int []weight = new int;

// Creating a function to create the sieve and check whether it is prime.
static void SieveOfEratosthenes(boolean prime[],
int p_size)
{
//If the prime say [p] is not changed, it is a prime number.
prime = false;
prime = false;

for (int p = 2; p * p <= p_size; p++)
{
// We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
if (prime[p])
{
// We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
for (int i = p * 2;
i < p_size; i += p)
prime[i] = false;
}
}
}
// Now, we have to create a function that will give us the sum of the digits of the n
static int digitSum(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
// Now, we have to create a function for the dfs
static void dfs(int __nod,
int parent,
boolean prime[])
{
//If the sum of the digits turns out to be the weight of the current node, then it is prime, and then we have to increase the answer.
int sum = digitSum(weight[__nod]);
if (prime[sum])

for (int to : graph[__nod])
{
if (to == parent)
continue;
dfs(to, __nod, prime);
}
}
// writing the main code to test the above functions
public static void main(String[] args)
{
// Extracting the weight of the nodes
weight = 144;
weight = 1234;
weight = 21;
weight = 5;
weight = 77;
for (int i = 0; i < graph.length; i++)
graph[i] = new Vecc<Integer>();
// Evaluating the edges of the tree

boolean []prime = new boolean[MAX];
Arrays.fill(prime, true);
SieveOfEratosthenes(prime, MAX);
dfs(1, 1, prime);
}
}``````

Output: Example 4)

``````# Writing a Python program that will count the nodes which contain the prime digit and hold the sum of the weights in a binary tree
from typing import List
MAX = 1000000

graph = [[] for _ in range(100)]
weight = [0 for _ in range(100)]

# Creating a function that will create the sieve and check whether it is prime.
def SieveOfEratosthenes(prime: List[bool], p_size: int) -> None:

# If the value we get is false, then it will indicate that the value is not prime
prime = False
prime = False

p = 2
while p * p <= p_size:

# If the prime say [p] is not changed, it is a prime number.
if (prime[p]):

# We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
for i in range(p * 2, p_size + 1, p):
prime[i] = False
p += 1

# Now, we have to create a function that will give us the sum of the digits of the n
def digitSum(n: int) -> int:
sum = 0
while (n):
sum += n % 10
n = n // 10
return sum

# Now, we have to create a function for the dfs
def dfs(__nod: int, parent: int, prime: List[bool]) -> None:
# If the sum of the digits turns out to be the weight of the current node, then it is prime, and then we have to increase the answer.
sum = digitSum(weight[__nod])
if (prime[sum]):
for to in graph[__nod]:
if (to == parent):
continue
dfs(to, __nod, prime)

# Writing the main code to test the above functions
if __name__ == "__main__":

# Extracting the weight of the nodes
weight = 144
weight = 1234
weight = 21
weight = 5
weight = 77

# Evaluating the edges of the tree
graph.append(2)
graph.append(3)
graph.append(4)
graph.append(5)

prime = [True for _ in range(MAX + 1)]
SieveOfEratosthenes(prime, MAX)
dfs(1, 1, prime)

Output: Example 5)

``````<script>
// Writing a Javascript program that will count the nodes which contain the prime digit and hold the sum of the weights in a binary tree
let MAX = 1000000;

let graph = [];

for(let i = 0; i < 100; i++){
graph.push([])
}

console.log(graph)
let weight = new Array(100);

// Creating a function to create the sieve and check whether it is prime.
function SieveOfEratosthenes(prime, p_size)
{
// If the value we get is false, then it will indicate that the value is not prime
prime = false;
prime = false;

for (let p = 2; p * p <= p_size; p++) {
//If the prime say [p] is not changed, it is a prime number.
if (prime[p]) {
// We have to progress all the multiples that we have of p, and then we have to put them to the non-prime number
for (let i = p * 2;
i <= p_size;
i += p)
prime[i] = false;
}
}
}

// Now, we have to create a function that will give us the sum of the digits of the n
function digitSum(n)
{
let sum = 0;
while (n) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
// Now, we have to create a function for the dfs
function of(__nod, parent, prime)
{
// In case the sum of the digits turns out to be the weight of the current node, then it is prime, and then we have to increase the answer.
let sum = digitSum(weight[__nod]);
if (prime[sum])

for (let to of graph[__nod]) {
if (to == parent)
continue;
dfs(to, __nod, prime);
}
}
// writing the main code to test the above functions
// Extracting the weight of the nodes
weight = 144;
weight = 1234;
weight = 21;
weight = 5;
weight = 77;

// Evaluating the edges of the tree
graph.push(2);
graph.push(3);
graph.push(4);
graph.push(5);

let prime = new Array(MAX);
prime.fill(true)

SieveOfEratosthenes(prime, MAX);

dfs(1, 1, prime); 