views:

1043

answers:

15

I tried myself to solve but I could not get any clue.

Please help me to solve this.

+1  A: 

Sounds like homework :)

I image you can write a function which iteratively divides a number. E.g. you can model what you do with a pen and a piece of paper to divide numbers. Or you can use shift operators and + to figure out if your intermediate results is too small/big and iteratively apply corrections. I'm not going to write down the code though ...

A: 

Slow and naive, but it should work, if an exact divisor exists. Addition is allowed, right?

for number from 1 to input
  if number == input+input+input
    return number

Extending it for fractional divisors is left as an exercise to the reader. Basically test for +1 and +2 I think...

Wouter Lievens
no, this always returns input (mult instead of divide and wrong loop range) and assumes input > 0
jk
Uhm, this doesn't do division at all and will never return anything."number" can be at most "input", so "number == input + input + input" will never be true.
kigurai
you should replace the 'number == input+input+input' by 'input == number+number+number'
Fortega
A: 

for positive integer division

result = 0
while (result + result + result < input)
 result +=1
return result
jk
Only integer division. Does not handle negative numbers. If input is is large a lot of unneccesary additions are made.
kigurai
yep but it does actually work with those constraints in contrast to another similar answer
jk
Yes, it is much better in that regard :)
kigurai
A: 

How about this, in some kind of Python like pseudo-code. It divides the answer into an integer part and a fraction part. If you want to convert it to a floating point representation then I am not sure of the best way to do that.

 x = <a number>  
 total = x
 intpart = 0
 fracpart = 0

 % Find the integer part
 while total >= 3
    total = total - 3
    intpart = intpart + 1

 % Fraction is what remains
 fracpart = total


 print "%d / 3 = %d + %d/3" % (x, intpart, fracpart)

Note that this will not work for negative numbers. To fix that you need to modify the algorithm:

  total = abs(x)
  is_neg = abs(x) != x

  ....

  if is_neg
      print "%d / 3 = -(%d + %d/3)" % (x, intpart, fracpart)
kigurai
+2  A: 
if(number<0){ // Edited after comments
number = -(number);
}
quotient = 0;
while (number-3 >= 0){ //Edited after comments..
number = number-3;
quotient++;
}//after loop exits value in number will give you reminder

EDIT: Tested and working perfectly fine :(

Hope this helped. :-)

Richie
Does not handle negative numbers
kigurai
It will be better if while condition will be `number >= 3` because you can get this: `5:3=2`
Gaim
thanks for the comments.. :-)
Richie
Does still not work. It will return one too much.Try number=3, you will notice how it will return quotient=2 instead of quotient = 1
kigurai
I don't think 5:3=2 ...5:3=1,666666...
Fortega
@kigurai : it wouldnt return quotient = 2 for number=3.. check again :( ..else my eyes and my brain should be tricking me.. i even tested the same.. please check the code thoroughly before putting comments and down voting.. pls
Richie
@Fortega 5/3 is 1.66666..i agree...here 1 is quotient.. if you check my code it gives you quotient as 1 and reminder as 2.. 2/3 is 0.66666...so both together will give you 1.6666..
Richie
@Richie: You are correct. Not sure what I was thinking :)
kigurai
+1  A: 

EDIT: Oops, I misread the title's question. Multiply operator is forbidden as well.

Anyway, I believe it's good not to delete this answer for those who didn't know about dividing by non power of two constants.


The solution is to multiply by a magic number and then to extract the 32 leftmost bits:

divide by 3 is equivalent to multiply by 1431655766 and then to shift by 32, in C:

int divideBy3(int n)
{
  return (n * 1431655766) >> 32;
}

See Hacker's Delight Magic number calculator.

Gregory Pakosz
Multiply is not allowed
Gaim
Read the title: "without using `/`, `%` and `*`".
KennyTM
@Gaim: yes I just misread the title. I was editing my answer while you were writing your comment
Gregory Pakosz
I pity the fool that has to pick that up for maintenance... :)
Paolo
isn't this non-portable?
Hogan
@Paolo: with commented code, it might not be a problem. It's good to have it in your optimization bag of tricks, and well parts of the code that are not really performance critical must not use it obviously
Gregory Pakosz
I like this answer. The multiplication can be rewritten as a for(int i = 0; i < 1431655766) n += n;Than the answer is in line with the requirements.
Fortega
thank you for the hackers delight link
jlru
@Fortega: `x += n;`. `n += n` will result in `n * 2^1431655766` :)
KennyTM
A: 

If you can use bitwise o

proba
+1  A: 

Hi

This is very easy, so easy I'm only going to hint at the answer --

Basic boolean logic gates (and,or,not,xor,...) don't do division. Despite this handicap CPUs can do division. Your solution is obvious: find a reference which tells you how to build a divisor with boolean logic and write some code to implement that.

Regards

Mark

High Performance Mark
I should have added, that if you want you could implement a circuit that only divides by 3, but why not implement a circuit for general division.
High Performance Mark
well divide by a constant is going to use less gates - often an important optimization in hardware design (guess it depends on what class this is for)
jk
+7  A: 

Using the mathematical relation:

1/3 == Sum[1/2^(2n), {n, 1, Infinity}]

We have

int div3 (int x) {
   int64_t blown_up_x = x;
   for (int power = 1; power < 32; power += 2)
      blown_up_x += ((int64_t)x) << power;
   return (int)(blown_up_x >> 33);
}

If you can only use 32-bit integers,

int div3 (int x) {
     int two_third = 0, four_third = 0;
     for (int power = 0; power < 31; power += 2) {
        four_third += x >> power;
        two_third += x >> (power + 1);
     }
     return (four_third - two_third) >> 2;
}

The 4/3 - 2/3 treatment is used because x >> 1 is floor(x/2) instead of round(x/2).

KennyTM
If this works then it is a really cool solution!
kigurai
The "64-bit" version *does* work. The "32-bit" version will at most be off by 1 or -1.
KennyTM
this is just binary representation of 1/3
ralu
+6  A: 

Are you supposed to use itoa() for this assignment? Because then you could use that to convert to a base 3 string, drop the last character, and then restore back to base 10.

Stuart Dunkeld
+5  A: 

Here's a solution implemented in C++:

#include <iostream>

int letUserEnterANumber()
{
    int numberEnteredByUser;
    std::cin >> numberEnteredByUser;
    return numberEnteredByUser;
}

int divideByThree(int x)
{
    std::cout << "What is " << x << " divided by 3?" << std::endl;
    int answer = 0;
    while ( answer + answer + answer != x )
    {
        answer = letUserEnterANumber();
    }
}

;-)

stakx
A: 
unsigned int  div3(unsigned int m) {
   unsigned long long n = m;
   n += n << 2;
   n += n << 4;
   n += n << 8;
   n += n << 16;
   return (n+m) >> 32;
}
fesno
+6  A: 

x/3 = e^(ln(x) - ln(3))

mbeckish
A: 

Convert 1/3 into binary

so 1/3=0.01010101010101010101010101

and then just "multiply" whit this number using shifts and sum

ralu
A: 
long divByThree(int x)
{    
  char buf[100];
  itoa(x, buf, 3); 
  buf[ strlen(buf) - 1] = 0; 
  char* tmp; 
  long res = strtol(buf, &tmp, 3);

  return res;
}