Ethiopian multiplication: Difference between revisions

→‎{{header|Java}}: I can;t believe it went that long with no one catching this error, added negative check to first version
(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:
import java.util.Map;
import java.util.Scanner;
public class Mult {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Line 1,080:
int second = sc.nextInt();
 
if(first < 0){
Map <Integer, Integer> columns = new HashMap <Integer, Integer>();
first = -first;
second = -second;
}
 
Map <Integer, Integer> columns = new HashMap <Integer, Integer>();
columns.put(first, second);
do{
first = doubleInthalveInt(first);
second = halveIntdoubleInt(second);
columns.put(first, second);
}while(first != 1);
 
int sum = 0;
for(Map.Entry <Integer, Integer> entry : columns.entrySet()){
if(!isEven(entry.getKey())){
sum += entry.getValue();
Line 1,106 ⟶ 1,111:
 
public static boolean isEven(int num){
return (num & 1) == 0;
}
}</lang>
Line 1,133 ⟶ 1,138:
/**
* This method is an improved version that will use
* ethiopian styled multiplication, butand can fullyalso
* supportsupports negative parameters.
* @param a Any integer.
* @param b Any integer.
Anonymous user