[solved] Simple math question ?
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
[solved] Simple math question ?
dim as integer n = 100 ' any number > 0
dim as integer x = (n * (n - 1) ) / 2
if x is 20_000_000 what was n ? (note: integer not floating point)
if it was x=(n*n)*0.5 then n = sqr(x*2)
but (n*(n-1))*0.5 makes me confuse ATM :-)
Joshy
dim as integer x = (n * (n - 1) ) / 2
if x is 20_000_000 what was n ? (note: integer not floating point)
if it was x=(n*n)*0.5 then n = sqr(x*2)
but (n*(n-1))*0.5 makes me confuse ATM :-)
Joshy
Last edited by D.J.Peters on Oct 23, 2020 14:28, edited 1 time in total.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Simple math question ?
n as integer it's sqr(x*2) !
Joshy
Joshy
Code: Select all
dim as integer n = 100
dim as integer x = n*(n-1)*0.5
print "n: " & n & " x: " & x
print
x = 20000000
n = sqr(x*2)
dim as integer y = n*(n-1)*0.5
print "n: " & n & " of x: " & x & " ~: " & y
n+=1 : y = n*(n-1)*0.5
print "n+1: " & n & " of x: " & x & " ~: " & y & " n+1 is to high"
sleep
Re: [solved] Simple math question ?
Try this:
If you look at the equation mathematically, then it has 2 solutions, because the function is a polynomial of second degree.Dim As Ulong x, n = 15
x = (n * (n - 1) ) / 2
? x, Sqr((2 * x) + 0.25) + 0.5 = n
Sleep
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: [solved] Simple math question ?
This works for me thank you all for the hints.
Joshy
Joshy
Code: Select all
function fx(n as integer) as integer
if n<2 then return 0
return (n*(n-1))*0.5
end function
function fn(x as integer) as integer
dim as integer n = sqr(x*2)+1
dim as integer y = fx(n)
if y<=x then return n
return n-1
end function
dim as integer n = 100
dim as integer x = fx(n)
print "n: " & n & " x: " & x
n = fn(x)
dim as integer y = fx(n)
print "n: " & n & " of x: " & x & " ~: " & y
x = 20000000
n = fn(x)
y = fx(n)
print "n: " & n & " of x: " & x & " ~: " & y
sleep
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: [solved] Simple math question ?
That's not as trivial as I thought to visualise this stuff. Maybe by a plot in 3D such similar to as below.
Here is when x = n^2/2
Here is when x = n^2/2
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: [solved] Simple math question ?
Hi,
good find Joshy. You were right, both expressions below are very close (the very small difference will vanish with x becoming big) :
.5*sqr(1+8*x) = sqr(2*x)
Weird but true.
Edit: in fact now I see why. As always it looks simple at the end :)
0.5*sqr(1 + 8*x) = sqr(0.25 + 2*x) ...........
good find Joshy. You were right, both expressions below are very close (the very small difference will vanish with x becoming big) :
.5*sqr(1+8*x) = sqr(2*x)
Weird but true.
Code: Select all
for x as integer = 1 to 1000000
if (.5*(1+sqr(1+8*x)) - sqr(2*x) - .5)>0.04 then
print "x=";x, "diff=";(.5*(1+sqr(1+8*x)) - sqr(2*x) - .5)
end if
if x=1000000 then
print
print "x=";x, "diff=";(.5*(1+sqr(1+8*x)) - sqr(2*x) - .5)
end if
next x
?"end"
0.5*sqr(1 + 8*x) = sqr(0.25 + 2*x) ...........
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: [solved] Simple math question ?
Here are the real problem I have here .
The tokamak C++ physics lib used:
maxOverlappingPairs = ( nCollisionShapes * (nCollisionShapes-1) )/2
let say you have 100 cubes how many pairs of cubes exist they can collide ?
100*100 is wrong becouse the 100 cubes can't collide with it's self:
x = (100*(100-1))
but for example if cube-10 can collide with cube-15 this builds a pair and cube-15 and cube-10 also so :
x = (100*(100-1))/2 looks ok so far (don't define a collision pair twice)
I found out if the number of pairs > 20,000,000 (more than 6323 collision shapes) the allocation of more than 20,000,000 collision results crashed the C++ lib.
I must do an deeper look why the allocation of more then 20,000,000 collisin results fails.
EDIT: Note in reality maybe only 5-10 % of all collision shapes will collide so allocation of 1,000,000 is more than enough.
But there must be a point why the allocation of more than 20,000,000 fails.
Joshy
The tokamak C++ physics lib used:
maxOverlappingPairs = ( nCollisionShapes * (nCollisionShapes-1) )/2
let say you have 100 cubes how many pairs of cubes exist they can collide ?
100*100 is wrong becouse the 100 cubes can't collide with it's self:
x = (100*(100-1))
but for example if cube-10 can collide with cube-15 this builds a pair and cube-15 and cube-10 also so :
x = (100*(100-1))/2 looks ok so far (don't define a collision pair twice)
I found out if the number of pairs > 20,000,000 (more than 6323 collision shapes) the allocation of more than 20,000,000 collision results crashed the C++ lib.
I must do an deeper look why the allocation of more then 20,000,000 collisin results fails.
EDIT: Note in reality maybe only 5-10 % of all collision shapes will collide so allocation of 1,000,000 is more than enough.
But there must be a point why the allocation of more than 20,000,000 fails.
Joshy
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: [solved] Simple math question ?
The C++ lib does not used size_t to allocate stuff it used int = signed 32-bit and it becomes a negative number !
Joshy
Joshy
Code: Select all
const MAX_BODIES = 256*256 ' stacking bricks build a wall one row 256 bricks and 256 rows height
dim as long maxPairs = (MAX_BODIES*(MAX_BODIES-1))/2
print maxPairs
sleep
Re: [solved] Simple math question ?
Two Minute Papers video
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: [solved] Simple math question ?
Why do you spam this tread with commercial stuff ?dafhi wrote:Two Minute Papers video