Ethiopian multiplication: Difference between revisions
Content added Content deleted
(Added Pascal) |
(→{{header|Java}}: I can;t believe it went that long with no one catching this error, added negative check to first version) |
||
Line 1,074: | Line 1,074: | ||
import java.util.Map; |
import java.util.Map; |
||
import java.util.Scanner; |
import java.util.Scanner; |
||
public class Mult |
public class Mult{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Scanner sc = new Scanner(System.in); |
Scanner sc = new Scanner(System.in); |
||
Line 1,080: | Line 1,080: | ||
int second = sc.nextInt(); |
int second = sc.nextInt(); |
||
if(first < 0){ |
|||
⚫ | |||
first = -first; |
|||
second = -second; |
|||
} |
|||
⚫ | |||
columns.put(first, second); |
columns.put(first, second); |
||
do{ |
do{ |
||
first = |
first = halveInt(first); |
||
second = |
second = doubleInt(second); |
||
columns.put(first, second); |
columns.put(first, second); |
||
}while(first != 1); |
}while(first != 1); |
||
int sum = 0; |
int sum = 0; |
||
for(Map.Entry |
for(Map.Entry<Integer, Integer> entry : columns.entrySet()){ |
||
if(!isEven(entry.getKey())){ |
if(!isEven(entry.getKey())){ |
||
sum += entry.getValue(); |
sum += entry.getValue(); |
||
Line 1,106: | Line 1,111: | ||
public static boolean isEven(int num){ |
public static boolean isEven(int num){ |
||
return num & 1 == 0; |
return (num & 1) == 0; |
||
} |
} |
||
}</lang> |
}</lang> |
||
Line 1,133: | Line 1,138: | ||
/** |
/** |
||
* This method is an improved version that will use |
* This method is an improved version that will use |
||
* ethiopian styled multiplication, |
* ethiopian styled multiplication, and also |
||
* |
* supports negative parameters. |
||
* @param a Any integer. |
* @param a Any integer. |
||
* @param b Any integer. |
* @param b Any integer. |