2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

For positive triplet (n,x,y), n/(x*y)==n/x/y even in integer division

2
Last updated at Posted at 2017-05-11

For positive triplet (n,x,y), n/(x*y)==n/x/y even in integer division.
This should be useful to avoid overflow.
Let's prove this below.


if n.divmod(xy) is (a,b), n can be represented as:

n=axy+b (0<=b<xy)

On the other hand, if n.divmod(x) is (c,d) and c.divmod(y) is (e,f), n can be represented as:

n=cx+d (0<=d<x)
c=ey+f (0<=f<y)
n=x(ey+f)+d=exy+xf+d

Now suppose d is x-1 and f is y-1. Then xf+d becomes x(y-1)+x-1=xy-1 (<xy).
This means 0<=xf+d<xy and a(==n/(x*y))==e(==n/x/y) has been proved.
Also, in n/(x*y), x and y are exchangable. So n/x/y == n/y/x. (Q.E.D.)

検索用:自然数の3つ組(n,x,y)に対してn/(x*y)==n/x/yは整数除算でも常に成立しますので、オーバーフローを避けるのに役立つでしょう。

170513

Actually this lemma is used for icbrt (integer cubic-root) used in http://qiita.com/cielavenir/items/2a685d3080862f2c2c47 .

180127

For fear, I confirmed small case...

# !/usr/bin/ruby
(1..10000).each{|n|
	(1..100).each{|x|
		(1..100).each{|y|
			r0=n/(x*y)
			r1=n/x/y
			r2=n/y/x
			if r0!=r1 || r1!=r2
				raise [n,x,y].join(',')
			end
		}
	}
}

https://qiita.com/cielavenir/items/d419b448d1c65eae2868 is another example where this lemma is useful.

261126

It is quite late, but I was able to had Claude Code to write the Coq program (tested on Debian 13 The Coq Proof Assistant, version 8.20.1).

divdiv.v
(* Assisted-by: Claude Code Opus 5.5 *)

Require Import ZArith Lia.
Open Scope Z_scope.

(* Z.div is floor division, same as Ruby's Integer#/ and divmod. *)

(* 1. Using the standard library lemma. *)
Theorem div_div_lib : forall n x y, 0 < x -> 0 < y -> n / x / y = n / (x * y).
Proof.
  intros n x y Hx Hy.
  rewrite Z.div_div by lia. reflexivity.
Qed.

(* 2. Following the passage step by step. *)
Theorem div_div_manual : forall n x y, 0 < x -> 0 < y -> n / (x * y) = n / x / y.
Proof.
  intros n x y Hx Hy.
  (* n.divmod(x) = (c, d):  n = x*c + d,  0 <= d < x *)
  pose proof (Z.div_mod n x ltac:(lia)) as Hn.
  pose proof (Z.mod_pos_bound n x Hx) as Hd.
  (* c.divmod(y) = (e, f):  c = y*e + f,  0 <= f < y *)
  pose proof (Z.div_mod (n / x) y ltac:(lia)) as Hc.
  pose proof (Z.mod_pos_bound (n / x) y Hy) as Hf.
  set (c := n / x) in *.
  set (d := n mod x) in *.
  set (e := c / y) in *.
  set (f := c mod y) in *.
  (* Uniqueness of division: n = e*xy + (xf+d) with 0 <= xf+d < xy, so e = n/(xy). *)
  symmetry.
  apply Z.div_unique with (r := x * f + d).
  - (* 0 <= xf+d <= x(y-1) + (x-1) = xy-1 < xy *)
    left. split; nia.
  - (* n = x(ey+f) + d = exy + (xf+d) *)
    rewrite Hn at 1. rewrite Hc at 1. ring.
Qed.

(* Swapping x and y. *)
Corollary div_div_comm : forall n x y, 0 < x -> 0 < y -> n / x / y = n / y / x.
Proof.
  intros n x y Hx Hy.
  rewrite <- (div_div_manual n x y Hx Hy), <- (div_div_manual n y x Hy Hx).
  now rewrite Z.mul_comm.
Qed.

(* Sanity check: the hypotheses matter (negative divisors break it, as in Ruby). *)
Example counterexample : 5 / (-2) / (-3) <> 5 / ((-2) * (-3)).
Proof. compute. discriminate. Qed.
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?