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){
Map <Integer, Integer> columns = new HashMap <Integer, Integer>();
first = -first;
second = -second;
}

Map<Integer, Integer> columns = new HashMap<Integer, Integer>();
columns.put(first, second);
columns.put(first, second);
do{
do{
first = doubleInt(first);
first = halveInt(first);
second = halveInt(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 <Integer, Integer> entry : columns.entrySet()){
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, but can fully
* ethiopian styled multiplication, and also
* support negative parameters.
* supports negative parameters.
* @param a Any integer.
* @param a Any integer.
* @param b Any integer.
* @param b Any integer.