
Ha ha, the rest of us missed that.





<pre>import timeit
start = timeit.default_timer()
import math
def fequals(a, b):
return abs(ab) < 0.0000001
def matmult(a,b):
zip_b = list(zip(*b))
return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b))
for col_b in zip_b] for row_a in a]
def area(theta):
r = [[math.cos(theta), math.sin(theta), 0.0],
[math.sin(theta), math.cos(theta), 0.0],
[0.0, 0.0, 1.0]]
v = [[1], [1], [1]]
point = matmult(r, v)
return abs(point[0][0]*point[2][0])
def position(theta):
r = [[math.cos(theta), math.sin(theta), 0.0],
[math.sin(theta), math.cos(theta), 0.0],
[0.0, 0.0, 1.0]]
c_o= [[0.5, 0.0, 0.0],
[0.0, 0.5, 0.0],
[0.0, 0.0, 0.5]]
result = matmult(r, c_o)
return(result)
def OptimizeTheta(A):
if fequals(A, 1.0):
return position(0.0)
elif fequals(A, 1.414213):
return position(math.pi / 4.0)
l = 0.0
m = math.pi / 4.0
while True:
mid = (l + m)/2.0
a = area(mid)
if fequals(A, a):
return position(mid)
if a < A:
l = mid
else:
m = mid
t =int(input())
for i in range(t):
A = float(input())
cube = OptimizeTheta(A)
print("Case #{}:".format(i+1))
print(cube[0][0], cube[1][0], cube[2][0])
print(cube[0][1], cube[1][1], cube[2][1])
print(cube[0][2], cube[1][2], cube[2][2])
#GHOST999
stop = timeit.default_timer()
execution_time = stop  start
Beyond value 1.41...
it becomes extremely inefficient..
appreciate any help..





and this algorithm is ?
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein





In what language was the code written?






Im building a indoor putting ramp and it will be possible to hit straight puts but also I will create breaks for variation.
I asking for help for an algorithm and the easy part of this is that the slopes will be fixed.
Imagine this like a pooltable. If a raise one side with 3cm. Which is 3 degrees to make it easy.
How much do I have to change my aim point compared to the straight putt?
Summary:
What I would like for this in the beginning.
Distance 1,2,3 Meters.
Fixed break. (Means the slope is the same on the entire surface)
Break 1,2,3,4 degrees.
2 different speeds:
One speed that the ball just make it to the hole.
Second speed that the ball ends up 70 cm behind the ball?
Why different speeds?
To show and illustrate how the aim point changes depending on how hard you roll the ball.
/Martin






I have a stored function in postgresql which is based on a recursive algorithm however this approach has some limitation especially when it's a matter of scalability, so i would like to ask if it is possible to switch from a recursive approach to an iterative approach. With the end goal being to optimize the computation time of the function.
Any answer will be very much appreciated.
Thank you.





Determine the maximum "depth of recursion" and then you will at least have a some starting point.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal






Hello everyone!
I've a follow question: is there any way to optimize this algorithm, which recursively computes the square root of an positive integer?.
int mySqrt(int x, int left, int right)
{
static int result = 0;
if (left <= right)
{
int middle = left + (right  left) / 2;
if ((middle != 0 && middle <= x / middle) 
(middle == 0 && middle <= x / (middle + 1)))
{
result = middle;
return mySqrt(x, middle + 1, right);
}
else
{
return mySqrt(x, left, middle  1);
}
}
return result;
}
Thank you!





Why recursion? Binary search can be expressed as iteration, or, since you actually have a constant bound on the number of iterations here, an unrolled iteration. That would IMO still count as the same algorithm, just a different realization of it.
lnikon wrote: left + (right  left) / 2 This trick is to avoid overflow. There cannot be any overflow here anyway, since both left and right will be well below MAXINT (at most they'll be the square root of MAXINT). So you can write it the simple/shorter/faster way.
lnikon wrote: middle == 0 && middle <= x / (middle + 1) Is this even correct? Note that the right operand of the && effectively checks whether 0 <= x which is not useful since x is known to be nonnegative (precondition).
The whole zero business can be avoided by checking whether middle * middle > x (this multiplication cannot overflow unless incorrect bounds were passed), which by avoiding division is both "zerosafe" and more efficient.





Your answer is very useful. Thank you!





Use Newton's method[^].
You'll also need to use floatingpoint numbers, since most square roots are not integers.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
 Homer





A square root function should have only 1 parameter and your recursive function with left and right should be for private use only.
The only way to have middle=0, is if x=0 from the beginning.
So if you check for x=0, you will not have to continuously check if middle=0 or not.
As far as I can see the only result your function can give is 0.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein





Hi,
I am interested on how to convert a point in 3D space (x,y,z) to 2D Space (x,y) using perspective accurate projection. I have found lots of things online showing how to do this, but they all use a bunch of stuff I don't understand like matrixes. I am only a grade 9 student so I don't know a lot of advanced math. If you guys could explain it to me in a simple way like:
x' = (something with x and z)
y' = (something with y and z)
That would be awesome
Thanks in advance everyone.
Sidenote, if you want to send me some example code please do it in python its the only language I'm any good at





I'm sorry ... but if you want to create a pseudo3DPoint inside a 2DView you need to know "a little bit" about Matrixes and vectorcalculation.
Normally your pseudo3dAxises are something like this :
Z
 Y
 /
/
X
.... but as a real 3DPoint the Yvector is not to be seen because it's in direction "into the screen".
So now, depending on your viewpoint (or camerapoint) you have to calculate it's new position inside the 2DView.
I hope I could explain what you need to do ...





I am going to agree with that answer.
There is quite a bit of basic math along with matrix math that one must master.
And one must also understand how to construct entities in a 3D space just so they can be rendered into 2D.
A conceptual understanding of the difficulties might be acquired by looking into the following,
Flatland  Wikipedia[^]





Thank you for your voting ...





I looked at this problem and assuming most node are behind a firewall that blocks port forwarding and the nodes are small machines then I just cannot come up with a plan that scales to millions of users that won't end up flooding the network with internal chatter.
My current thoughts are of specialized DNS type nodes working as a replicated cluster to provide the service and then dealing with subdomaining the address at a later date if needs be but this is not the way I like working and is made worse because the lookup's will be 100200 bytes in size.
As if things could not get any worse the data needs to be encrypted before being written to files but the values are 32 byte public keys that can be broken down and map 35 X 35 X 35 files which seems like the starting point.





I sometimes send an email to myself.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





I have a problem with my homework.
Description:
1. In the group of people we define the relationship: A does not like B. This relation is not symmetrical.
If there is a series of relationships A1 does not like A2, A2 does not like A3 ... Ak does not like A1,
all people in this cycle belong to one group of "disliking".
For the given pair specifying the relations, determine the maximum division
groups of "disliking". Provide complexity and justify the correctness of your solution.
2. There are some animosities in the group of naughty children. The preschool teacher decided to
set the children in a row. If A does not like B, he can not stand in a row in front of B (not to throw papers in it).
a) Specify if a group of children can be set in one row.
b) If not, enter the minimum number of rows to set children in above configuration.
Provide complexity and justify the correctness of your solution.
c) Enter the minimum number of rows necessary to set the children, if:
Child A does not like child B, it must stand in a row with a lower number.
I know that in the first task I have to find strongly connected components. Actually I did it using Tarjan's algorithm in PHP implementations. I wonder that is it correct approach and how to provide complexity of my solution. Unfortunately I don't have any information how to solve second task. Maybe someone has an idea and could give a clue.
Thanks for advice
Code of first task:
const GROUPS_COUNTER = 10;
const MIN_ASCII = 65;
const MAX_ASCII = 69;
function generateGroups($n) {
$matrix = [];
$exist = [];
echo "************************\n";
for ($i = 0; $i < $n; $i++) {
$a = chr(rand(MIN_ASCII, MAX_ASCII));
$b = chr(rand(MIN_ASCII, MAX_ASCII));
if ($a == $b  isset($exist[$a][$b])  isset($exist[$b][$a])) {
$i;
continue;
}
$matrix[$i][0] = $a;
$matrix[$i][1] = $b;
$exist[$a][$b] = true;
echo $a . " " . $b . "\n";
}
echo "************************\n";
return $matrix;
}
function findGroups($groups) {
$dislikeList = [];
$checked = [];
foreach ($groups as $groupParent) {
if (isset($checked[$groupParent[0]])) {
continue;
}
$groupChildList = [];
foreach ($groups as $groupChild) {
if ($groupParent[0] == $groupChild[0]) {
array_push($groupChildList, $groupChild[1]);
}
}
echo $groupParent[0] . " => " . implode(", ", $groupChildList) . "\n";
$dislikeList[$groupParent[0]] = $groupChildList;
$checked[$groupParent[0]] = true;
}
echo "************************\n";
return $dislikeList;
}
function phpTarjanEntry($groups) {
global $cycles;
global $marked;
global $markedStack;
global $pointStack;
$cycles = array();
$marked = array();
$markedStack = array();
$pointStack = array();
$marked = array();
foreach ($groups as $key => $value) {
$marked[$key] = FALSE;
}
foreach ($groups as $key => $value) {
phpTarjan($key, $key);
while (!empty($markedStack)) {
$marked[array_pop($markedStack)] = FALSE;
}
}
$cycles = array_keys($cycles);
return $cycles;
}
function phpTarjan($s, $v) {
global $G;
global $cycles;
global $marked;
global $markedStack;
global $pointStack;
$f = FALSE;
$pointStack[] = $v;
$marked[$v] = TRUE;
$markedStack[] = $v;
foreach ($G[$v] as $w) {
if ($w < $s) {
$G[$w] = array();
} else if ($w == $s) {
$cycles[implode('', $pointStack)] = TRUE;
$f = TRUE;
} else if (isset($marked[$w]) && $marked[$w] === FALSE) {
$g = phpTarjan($s, $w);
if (!empty($f) OR ! empty($g)) {
$f = TRUE;
}
}
}
if ($f === TRUE) {
while (end($markedStack) != $v) {
$marked[array_pop($markedStack)] = FALSE;
}
array_pop($markedStack);
$marked[$v] = FALSE;
}
array_pop($pointStack);
return $f;
}
$mixGroups = generateGroups(GROUPS_COUNTER);
$res = findGroups($mixGroups);
$G = $res;
$cycles = phpTarjanEntry($res);
echo '<p>Cycles found: ' . count($cycles);
print_r($cycles);





Hi, everyone, I am not a software developer or coder, but I have a very interesting question for you!
I work for a cargo airline, our cargo area inside aircraft is a cylinder with Radius of 30 inches and length of 332 inches, with a flat floor on bottom of 44 inches.
Now, here is the question,
I have a customer have many boxes with dimension of x*y*z,
lets say the box dimension is 24*18*14 and I want to know the max amount of boxes I can put in the aircraft, and how?





Member 13644847 wrote: I am not a software developer or coder, but I have a very interesting question for you! No, of course not.
See knapsack problem  Google Search[^]





Member 13644847 wrote: I am not a software developer or coder,...I want to know the max amount of boxes I can put in the aircraft, and how?
Then hire a mathematician. Or even a consulting company.




