## Largest Possible Remainder

### October 16, 2015

The result of dividing a number *n* by a divisor *d* is a quotient *q* and a remainder *r*. Given a fixed *n* and a divisor d in the range 1 ≤ *d* < *n*, find the divisor that produces the largest possible remainder. For instance, given *n* = 20 and 1 ≤ *d* < 10, the largest remainder is 6 when the divisor *d* is 7.

Your task is to write a program that finds the largest possible remainder. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Emacs Lisp

Using cute from srfi-26

and range from standard prelude

Haskell:

Scala dumm algo:

Two versions in Python. The second (lpr) uses the fact that there are maxima close to n/2, n/3, … Ths version is a lot faster for larger n and d.

count is from the itertools lib and isqrt is the integer square root.

Two solutions, both in Racket Scheme.

Racket (tail recursive using default parameters)

[…] Largest Possible Remainder problem in racket: […]

#include

int main()

{

int i,num,temp,rem=0;

printf(“Enter The Num\n”);

scanf(“%d”,&num);

for(i=2;irem)

rem = num%i;

printf(“Max remider = %d\n”,rem);

return 0;

}