Logical AND (&&)
While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false.
C++14
#include <bits/stdc++.h>
using namespace std;
bool isOdd(int n) { return (n & 1); }
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if ((n % i) == 0)
return false;
return true;
}
int main()
{
int cnt = 0, n = 10;
for (int i = 2; i <= n; i++) {
if (isOdd(i) && isPrime(i))
cnt++;
}
cnt = 0;
n = 10;
for (int i = 2; i <= n; i++) {
if (isPrime(i) && isOdd(i))
cnt++;
}
}
|
Java
public class Main {
public static boolean isOdd(int n) {
return n % 2 != 0;
}
public 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;
}
public static void main(String[] args) {
int cnt = 0;
int n = 10;
for (int i = 2; i < n; i++) {
if (isOdd(i) && isPrime(i)) {
cnt++;
}
}
System.out.println("Implementation 1: " + cnt);
cnt = 0;
n = 10;
for (int i = 2; i < n; i++) {
if (isPrime(i) && isOdd(i)) {
cnt++;
}
}
System.out.println("Implementation 2: " + cnt);
}
}
|
Python3
def isOdd(n):
pass
def isPrime(n):
pass
if __name__ == '__main__':
cnt = 0; n = 10
for i in range(2,n) :
if isOdd(i) and isPrime(i):
cnt+=1
cnt = 0
n = 10
for i in range(2,n):
if isPrime(i) and isOdd(i):
cnt+=1
|
C#
using System;
class MainClass {
public static bool isOdd(int n) {
return n % 2 != 0;
}
public static bool 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;
}
public static void Main() {
int cnt = 0;
int n = 10;
for (int i = 2; i < n; i++) {
if (isOdd(i) && isPrime(i)) {
cnt++;
}
}
Console.WriteLine("Implementation 1: " + cnt);
cnt = 0;
n = 10;
for (int i = 2; i < n; i++) {
if (isPrime(i) && isOdd(i)) {
cnt++;
}
}
Console.WriteLine("Implementation 2: " + cnt);
}
}
|
Javascript
<script>
function isOdd(n) { return (n & 1); }
function isPrime(n)
{
if (n <= 1)
return false;
for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
if ((n % i) == 0)
return false;
return true;
}
let cnt = 0, n = 10;
for (let i = 2; i <= n; i++) {
if (isOdd(i) && isPrime(i))
cnt++;
}
cnt = 0;
n = 10;
for (let i = 2; i <= n; i++) {
if (isPrime(i) && isOdd(i))
cnt++;
}
</script>
|
Consider the above implementation:
In implementation 1, we avoid checking even numbers whether they are prime or not as primality test requires more computation than checking a number for even/odd.
Probability of a number getting odd is more than of it being a prime that’s why we first check whether the number is odd before checking it for prime.
On the other hand in implementation 2, we are checking whether the number is prime or not before checking whether it is odd which makes unnecessary computation as all even numbers other than 2 are not prime but the implementation still checks them for prime.
Logical OR (||)
While using || (logical OR), we must put the condition first whose probability of getting true is high so that compiler doesn’t need to check the second condition if the first condition is true.
C
#include <stdio.h>
bool isEven(int n);
bool isPrime(int n);
int main()
{
int cnt = 0, n = 10;
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
}
|
Java
import java.io.*;
class GFG {
boolean isEven(int n);
boolean isPrime(int n);
public static void main (String[] args)
{
int cnt = 0, n = 10;
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
}
}
|
Python3
def isEven(n):
pass
def isPrime(n):
pass
if __name__ == '__main__':
cnt = 0; n = 10
for i in range(3,n) :
if isOdd(i) or not isPrime(i):
cnt+=1
|
Javascript
function isEven(n);
function isPrime(n);
let cnt = 0, n = 10;
for (let i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
|
C#
using System;
class GFG {
bool isEven(int n)
{
return (n % 2 == 0);
}
bool 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 void Main(string[] args)
{
int cnt = 0, n = 10;
for (int i = 3; i <= n; i++) {
if (new GFG().isEven(i) || !new GFG().isPrime(i))
cnt++;
}
}
}
|
C++
#include <iostream>
using namespace std;
bool isEven(int n) {
return (n % 2 == 0);
}
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int cnt = 0, n = 10;
for (int i = 3; i <= n; i++) {
if (isEven(i) || !isPrime(i))
cnt++;
}
return 0;
}
|
As described earlier that the probability of a number being even is more than that of it being a non-prime. The current order of execution of the statements doesn’t allow even numbers greater than 2 to be checked whether they are non-prime (as they are all non-primes).
Note: For larger inputs, the order of the execution of statements can affect the overall execution time for the program.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!