Programming Articles - Page 1752 of 3366

Check if a number is Primorial Prime or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:55

832 Views

Suppose we have a number n, we have to check whether n is a primorial prime or not. A number is said to be a primorial prime when it is a prime number of the form pN# + 1 or pN# – 1 , where pN# indicates the primorial of pN such that the product of first N prime numbers.So, if the input is like 29, then the output will be True as 29 is Primorial prime of the form pN - 1 if N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.To solve this, we will follow these ... Read More

Gaussian Filter Generation in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:20

2K+ Views

As we know the Gaussian Filtering is very much useful applied in the field of image processing. It is used to reduce the noise of an image. In this section we will see how to generate a 2D Gaussian Kernel. Gaussian Distribution for generating 2D kernel is as follows.$$G(x,y)= \frac{1}{2\Pi\:\sigma^{2}}e^{\frac{x^{2}+y^{2}}{2\sigma^{2}}}$$ExampleLet us see the following implementation to get better understanding − Live Demo#include #include #include #define PI 3.1415 using namespace std; void calc_filter(double kernel[][5]) {    double sigma = 1.0;    double p, q = 2.0 * sigma * sigma;    double sum = 0.0;    for (int x = -2; x

Computing index using pointers returned by STL functions in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:39:36

183 Views

In this section we will see how to generate index using pointers that are returned by STL in C++. As we know many inbuilt functions in C++ return the pointers to the position in memory which provides the address of desired number, but it has no relation with the actual index in the container of the returned value. As an example, to determine maximum element in a code, we use std::max_element() function, this does not return the index of the desired element, but return the address in memory. But sometimes, we need to get the index from that address. Here ... Read More

Check if a number is an Achilles number or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:38:56

293 Views

Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is ... Read More

How to make a C++ class whose objects can only be dynamically allocated?

Arnab Chakraborty
Updated on 27-Aug-2020 13:37:16

362 Views

In this problem we will see how we can make one class for which we can only create objects through dynamic memory allocation, no direct object creation is permitted.The idea is simple. We have to create private destructor for that class. When the destructor is private, the compiler would produce a compiler error for non-dynamically allocated objects because compiler need to remove them from stack segment once they are not available for use. For dynamically allocated objects, the programmer is responsible for deleting object, but the compiler is not responsible for that, so it permits creating objects dynamically.For avoiding memory ... Read More

Why is the size of an empty class not zero in C++?

Ravi Ranjan
Updated on 15-Apr-2025 17:31:12

2K+ Views

The size of an empty class is not zero in C++ as it allocates one unique address to the object in the memory. The size can not be 0, because the two classes can not have the same memory address. So, the size of an empty class is 1 byte which does not hold any data, it is just for memory allocation. In this article, we will see an example of checking the size of an object of an empty class in C++. Demonstrating Size of an Empty Class In this approach, we have two C++ classes. One class ... Read More

Check if a number is a Trojan Numbers in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:35:01

156 Views

Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More

Where is an object stored if it is created inside a block in C++?

Arnab Chakraborty
Updated on 27-Aug-2020 13:30:13

1K+ Views

In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.Heap − When some objects are allocated dynamically, then that is stored inside the heap section.The scope of the object declared inside a block or a function is ... Read More

Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:26:52

411 Views

Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9

Check if a key is present in every segment of size k in an array in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:23:50

181 Views

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More

Advertisements