Задачи по программированию на шахматной доске. Chessboard programming problems. And other problems.

Arranged probability

	If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, 
	it can be seen that the probability of taking two blue discs, P(BB) = (15/21)×(14/20) = 1/2.

	The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing 
	eighty-five blue discs and thirty-five red discs.

	By finding the first arrangement to contain over 1012 = 1,000,000,000,000 discs in total, determine 
	the number of blue discs that the box would contain.
	

Solution in Python Let $x$ be blue disks and $y$ be total number of disks. $P(BB) = \dfrac{x(x-1)}{y(y-1)} = \dfrac{1}{2} (1)$ $y > 10^{12} (2)$ $x$ and $y$ are too large to compare with 1. Therefore, equation (1) can be rewritten $P(BB) = \dfrac{x^2}{y^2} = \dfrac{1}{2} (3)$ The program in Python will iterate $ 10^{13} > y > 10^{12} (4)$ and x iteration range can be seen as $ \dfrac{y}{\sqrt{2}} < x < 10 \dfrac{y}{\sqrt{2}} (5)$ based on equeation (3).
	import datetime
	import math

	def MainProc1(y):
		dtStart = datetime.datetime.now()

		print ("Started at: ", dtStart)

		print ("y = ", y, " the power, log(y), is ", math.ceil(math.log(y, 10)))

		nCounter = 0

		bSolution = False

		for j in range (y, y * 10, 1):
			nTemp = j * (j - 1)

			nTemp1 = math.floor(j/math.sqrt(2))

			for i in range (nTemp1, (nTemp1 * 10), 1):
				nTemp2 = 2 * i * (i - 1)

				nCounter += 1

				if nTemp == nTemp2 :
					print(j, i, nTemp2, nTemp)
					print("Solution !!")
					bSolution = True
					break
				elif nTemp < nTemp2 :
					break

			if bSolution == True:
				break

		dtEnd = datetime.datetime.now()

		print("The number of iterations are ", nCounter)

		print("Start time is ", dtStart.time(), " End time is ", dtEnd.time())
		print ("Time span is %s"%(dtEnd-dtStart))

		if bSolution == False :
			print("No solutions are found.")
		else :
			print("*** The solution is found. ***", j, i)

	#################
	MainProc1(math.floor(math.pow(10, 12)))
	
The program finds solution in 2.5 days. Started at: 20xx-08-16 17:04:02.630697 y = 1000000000000 the power, log(y), is 12 1070379110497 756872327473 1145711440187278556476512 1145711440187278556476512 Solution !! The number of iterations are 151064229087 Start time is 17:04:02.630697 End time is 05:44:49.463610 Time span is 2 days, 12:40:46.832913 *** The solution is found. *** 1070379110497 756872327473

$x=756872327473 \approx 10^{11.879}$ $y=1070379110497 \approx 10^{12.029}$

Комментарии ( 0 )

Сначала новые
Сначала старые
Сначала лучшие

АВТОРИЗУЙТЕСЬ ЧЕРЕЗ СОЦ.СЕТИ
ИЛИ ВОЙДИТЕ КАК ГОСТЬ

Войти