aboutsummaryrefslogtreecommitdiff
path: root/scripts/sqrt_int_guess.bc
blob: 9fabea874aebf67935818e1632f8950b809c097d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/bc -l
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2018-2025 Gavin D. Howard and contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# Adjust this number to try ranges above different powers of 10.
max = 0

n = (1 << max)

# Uncomment this to test the high part of the ranges.
#n += (1 - (1 >> 10))

n

# Loop from the start number to the next power of 10.
for (i = n; i < (n$ << 1); i += 1)
{
	# This is the lower limit.
	t1 = sqrt(1/(3*i))

	l = length(i$)/2

	print "i: ", i, "\n"
	#print "l: ", l, "\n"

	if (l$ != l)
	{
		# Limit between 2.4 and 3.
		limit = 2.7 << (l$ * 2)
		#print "limit: ", limit, "\n"

		if (i >= limit)
		{
			t2 = 1/(i >> (l$)) * 2
		}
		else
		{
			t2 = 1/(i >> (l$))
		}
	}
	else
	{
		# Limit between 3.8-ish and 4.8
		limit = 4.3 << (l$ * 2 - 1)
		#print "limit: ", limit, "\n"

		if (i >= limit)
		{
			t2 = 1/(i >> (l$ - 1)) * 8
		}
		else
		{
			t2 = 1/(i >> (l$ - 1)) * 4
		}
	}

	# This is the upper limit.
	t3 = sqrt(5/(3*i))

	# This is true when the guess is in between the limits.
	good = (t1 < t2 && t2 < t3)

	print t1, " < ", t2, " < ", t3, ": ", good, "\n"

	# Error if we have a problem.
	if (!good) sqrt(-1)
}

halt