Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 23-04-2014 10: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,141 ✭✭✭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