17
17
public class Multiply {
18
18
19
19
/**
20
- * Multiplies a number with 8 by performing 3 left shifts.
20
+ * Multiplies {@param n} with 3.5 and returns only the
21
+ * integral part of the number and omits the fractional part.
21
22
*
22
23
* @param n
23
24
* @return
24
25
*/
25
- public static long multiplyBy8 (long n ) {
26
- return (n << 3 );
26
+ public static long multiplyWith3point5 (long n ) {
27
+ return (n << 1 ) + n + ( n >> 1 ); // n*2 + n + n/2 = 7n/2 = 3.5n
27
28
}
28
29
29
30
/**
@@ -34,16 +35,31 @@ public static long multiplyBy8(long n) {
34
35
* @param n
35
36
* @return
36
37
*/
37
- public static long multiplyBy7 (long n ) {
38
+ public static long multiplyWith7 (long n ) {
38
39
return (n << 3 ) - n ;
39
40
}
40
41
42
+ /**
43
+ * Multiplies a number with 8 by performing 3 left shifts.
44
+ *
45
+ * @param n
46
+ * @return
47
+ */
48
+ public static long multiplyWith8 (long n ) {
49
+ return (n << 3 );
50
+ }
51
+
41
52
public static void main (String a []) {
42
- System .out .println (multiplyBy7 (4 ));
43
- System .out .println (multiplyBy7 (6 ));
44
- System .out .println (multiplyBy7 (7 ));
45
- System .out .println (multiplyBy8 (4 ));
46
- System .out .println (multiplyBy8 (6 ));
47
- System .out .println (multiplyBy8 (7 ));
53
+ System .out .println (multiplyWith3point5 (3 ));
54
+ System .out .println (multiplyWith3point5 (4 ));
55
+ System .out .println (multiplyWith3point5 (6 ));
56
+ System .out .println (multiplyWith3point5 (-7 ));
57
+
58
+ System .out .println (multiplyWith7 (6 ));
59
+ System .out .println (multiplyWith7 (7 ));
60
+
61
+ System .out .println (multiplyWith8 (4 ));
62
+ System .out .println (multiplyWith8 (6 ));
63
+ System .out .println (multiplyWith8 (7 ));
48
64
}
49
65
}
0 commit comments