Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Can I calculate floor(x/y) using only +, -, * and / ?

  • 23-04-2014 11:24AM
    #1
    Registered Users, Registered Users 2 Posts: 1,029 ✭✭✭


    Does anyone know if there's a way to calculate floor(x/y) using only the 4 operations; addition, subtraction, multiplication and division?

    For example if x=100 & y=7 then x/y = 14.29 and floor(x/y) = 14. Is there any function on x and y using only the 4 simple operators which will give me the the value 14 (or even a close approximation)?

    It's a puzzle I came across in work today so thanks in advance for any help!


Comments

  • Registered Users, Registered Users 2 Posts: 5,130 ✭✭✭Yakuza


    As a programming problem, you can use loop that subtracts 7 from 100 until the value goes below 0, and return one less than the number of iterations it takes to hit zero.

    You can also use a recursive function that keeps going until the divisor drops below zero.
    (snarfed from google:)
    int divide(int a, int b) {
    if (a < 0) return -divide(-a, b);
    if (b < 0) return -divide(a, -b);
    if (a < b) return 0;
    return 1 + divide(a - b, b);
    }


  • Registered Users, Registered Users 2 Posts: 1,029 ✭✭✭John_C


    Thanks for the reply.

    I don't have the use of conditional statements or recursive functions so it won't fit my needs. I can only use those 4 operators (and brackets to specify priority).


  • Closed Accounts Posts: 17 Miggle Kop


    I'm not sure if you can do this with just +, -, * and / because they treat all numbers equally and don't have any "preference" for whole numbers. If you can it's going to be some weird function because for example floor(100/7) will give the same answer as floor(100/7.1) and floor(100/7.01).


  • Registered Users, Registered Users 2 Posts: 4,893 ✭✭✭Davidius


    I doubt it is possible.

    If you keep x fixed and have the floor function equal a rational function P(y)/Q(y) where P, Q are polynomials then you have P(y)=0 for all y>x implying it has infinitely many roots which is a contradiction.


  • Registered Users, Registered Users 2 Posts: 642 ✭✭✭red_fox


    Not possible, the floor is not continuous, and any composition of =,-,* is continuous (and / usually is...)


  • Advertisement
  • Closed Accounts Posts: 17 Miggle Kop


    If it can be done it would have to be an infinite power series that approximates a discontinuous function like a Fourier series can do.


  • Registered Users, Registered Users 2 Posts: 1,029 ✭✭✭John_C


    Thanks for the replies. Those contradictions make sense so I guess it's not possible.


Advertisement