Efficiently multiply a number by 7
Efficiently multiplying a number by 7 can be achieved using bitwise and arithmetic operations. In this explanation, I will provide a suitable example, pseudocode, algorithm, and the resultant output. I'll also explain the time complexity of the code.
Multiplying a number by 7 can be done using traditional arithmetic multiplication, but there is a more efficient method that involves using bitwise shift operations. By understanding the binary representation of 7, we can take advantage of this knowledge to perform the multiplication in a faster way.
Problem Statement:
Given a number 'n,' we need to find an efficient method to multiply it by 7 and display the result.
Suitable Example
Let's consider the number 9 for this example. We want to multiply 9 by 7 efficiently.
Standard Pseudocode:
1. Start
2. Read n // Input the number to be multiplied by 7
3. result = (n << 3) - n // Efficient multiplication by 7 using bitwise shift
4. Display "The result of", n, "multiplied by 7 is", result
5. End
Algorithm
- Start the program.
- Read the value of 'n.'
- Perform the operation
(n << 3) - n
. a. Left shift 'n' by 3 positions, which is equivalent to multiplying 'n' by 2^3 (i.e., 8). b. Subtract 'n' from the result obtained in step 3a. c. Store the final result in a variable called 'result.' - Display the result of the multiplication: "The result of [input n] multiplied by 7 is [result]."
- End the program.
Explanation:
Let's take the example of 'n = 9' to understand how the algorithm works.
- We start with 'n = 9.'
- Perform the operation
(n << 3) - n
. a. Left shift 'n' by 3 positions: 9 << 3 = 72 (binary representation: 1001000). b. Subtract 'n' from the result: 72 - 9 = 63. - So, the result of 9 multiplied by 7 is 63.
Code Solution
Here given code implementation process.
// C program
// Efficiently multiply a number by 7
#include <stdio.h>
// Perform multiplication by seven
void multiplyBy7(int n)
{
// Equivalent to n *7
printf("\n %d X 7 = %d", n, ((n << 3) - n));
}
int main(int argc, char
const *argv[])
{
// Test Cases
multiplyBy7(-3);
multiplyBy7(10);
multiplyBy7(5);
multiplyBy7(13);
return 0;
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Java program
// Efficiently multiply a number by 7
public class Multiply
{
// Perform multiplication by seven
public void multiplyBy7(int n)
{
// Same as num * 7
System.out.print("\n" + n + " X 7 = " + ((n << 3) - n));
}
public static void main(String[] args)
{
Multiply task = new Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Include header file
#include <iostream>
using namespace std;
// C++ program
// Efficiently multiply a number by 7
class Multiply
{
public:
// Perform multiplication by seven
void multiplyBy7(int n)
{
// Same as num *7
cout << "\n" << n << " X 7 = " << ((n << 3) - n);
}
};
int main()
{
Multiply task = Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
return 0;
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Include namespace system
using System;
// C# program
// Efficiently multiply a number by 7
public class Multiply
{
// Perform multiplication by seven
public void multiplyBy7(int n)
{
// Same as num * 7
Console.Write("\n" + n + " X 7 = " + ((n << 3) - n));
}
public static void Main(String[] args)
{
Multiply task = new Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
<?php
// Php program
// Efficiently multiply a number by 7
class Multiply
{
// Perform multiplication by seven
public function multiplyBy7($n)
{
// Same as num * 7
echo "\n". $n ." X 7 = ". (($n << 3) - $n);
}
}
function main()
{
$task = new Multiply();
$task->multiplyBy7(-3);
$task->multiplyBy7(10);
$task->multiplyBy7(5);
$task->multiplyBy7(13);
}
main();
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Node Js program
// Efficiently multiply a number by 7
class Multiply
{
// Perform multiplication by seven
multiplyBy7(n)
{
// Same as num * 7
process.stdout.write("\n" + n + " X 7 = " + ((n << 3) - n));
}
}
function main()
{
var task = new Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
main();
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
# Python 3 program
# Efficiently multiply a number by 7
class Multiply :
# Perform multiplication by seven
def multiplyBy7(self, n) :
# Same as num * 7
print("\n", n ," X 7 = ", ((n << 3) - n), end = "")
def main() :
task = Multiply()
# Test Cases
task.multiplyBy7(-3)
task.multiplyBy7(10)
task.multiplyBy7(5)
task.multiplyBy7(13)
if __name__ == "__main__": main()
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
# Ruby program
# Efficiently multiply a number by 7
class Multiply
# Perform multiplication by seven
def multiplyBy7(n)
# Same as num * 7
print("\n", n ," X 7 = ", ((n << 3) - n))
end
end
def main()
task = Multiply.new()
# Test Cases
task.multiplyBy7(-3)
task.multiplyBy7(10)
task.multiplyBy7(5)
task.multiplyBy7(13)
end
main()
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Scala program
// Efficiently multiply a number by 7
class Multiply
{
// Perform multiplication by seven
def multiplyBy7(n: Int): Unit = {
// Same as num * 7
print("\n" + n + " X 7 = " + ((n << 3) - n));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiply = new Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Swift 4 program
// Efficiently multiply a number by 7
class Multiply
{
// Perform multiplication by seven
func multiplyBy7(_ n: Int)
{
// Same as num * 7
print( n ," X 7 = ", ((n << 3) - n));
}
}
func main()
{
let task: Multiply = Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
main();
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
// Kotlin program
// Efficiently multiply a number by 7
class Multiply
{
// Perform multiplication by seven
fun multiplyBy7(n: Int): Unit
{
// Same as num * 7
print("\n" + n + " X 7 = " + ((n shl 3) - n));
}
}
fun main(args: Array < String > ): Unit
{
var task: Multiply = Multiply();
// Test Cases
task.multiplyBy7(-3);
task.multiplyBy7(10);
task.multiplyBy7(5);
task.multiplyBy7(13);
}
Output
-3 X 7 = -21
10 X 7 = 70
5 X 7 = 35
13 X 7 = 91
These results are consistent with the algorithm. For example, taking '-3' as input, the algorithm follows the steps:
- Left shift '-3' by 3 positions: -3 << 3 = -24.
- Subtract '-3' from the result: -24 - (-3) = -21.
- So, the result of '-3' multiplied by 7 is -21.
Time Complexity
The time complexity of the given algorithm is constant (O(1)). It doesn't depend on the input size and will execute in a constant amount of time regardless of the value of 'n'. The algorithm involves only a few bitwise and arithmetic operations, so its execution time is very efficient and constant for any input.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment