This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,41 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Mult{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int first = sc.nextInt();
int second = sc.nextInt();
if(first < 0){
first = -first;
second = -second;
}
Map<Integer, Integer> columns = new HashMap<Integer, Integer>();
columns.put(first, second);
int sum = isEven(first)? 0 : second;
do{
first = halveInt(first);
second = doubleInt(second);
columns.put(first, second);
if(!isEven(first)){
sum += second;
}
}while(first > 1);
System.out.println(sum);
}
public static int doubleInt(int doubleMe){
return doubleMe << 1; //shift left
}
public static int halveInt(int halveMe){
return halveMe >>> 1; //shift right
}
public static boolean isEven(int num){
return (num & 1) == 0;
}
}

View file

@ -0,0 +1,51 @@
/**
* This method will use ethiopian styled multiplication.
* @param a Any non-negative integer.
* @param b Any integer.
* @result a multiplied by b
*/
public static int ethiopianMultiply(int a, int b) {
if(a==0 || b==0) {
return 0;
}
int result = 0;
while(a>=1) {
if(!isEven(a)) {
result+=b;
}
b = doubleInt(b);
a = halveInt(a);
}
return result;
}
/**
* This method is an improved version that will use
* ethiopian styled multiplication, and also
* supports negative parameters.
* @param a Any integer.
* @param b Any integer.
* @result a multiplied by b
*/
public static int ethiopianMultiplyWithImprovement(int a, int b) {
if(a==0 || b==0) {
return 0;
}
if(a<0) {
a=-a;
b=-b;
} else if(b>0 && a>b) {
int tmp = a;
a = b;
b = tmp;
}
int result = 0;
while(a>=1) {
if(!isEven(a)) {
result+=b;
}
b = doubleInt(b);
a = halveInt(a);
}
return result;
}

View file

@ -0,0 +1,27 @@
var eth = {
halve : function ( n ){ return Math.floor(n/2); },
double: function ( n ){ return 2*n; },
isEven: function ( n ){ return n%2 === 0); },
mult: function ( a , b ){
var sum = 0, a = [a], b = [b];
while ( a[0] !== 1 ){
a.unshift( eth.halve( a[0] ) );
b.unshift( eth.double( b[0] ) );
}
for( var i = a.length - 1; i > 0 ; i -= 1 ){
if( !eth.isEven( a[i] ) ){
sum += b[i];
}
}
return sum + b[0];
}
}
// eth.mult(17,34) returns 578