
-----------------------------------
sweetiechic
Thu Oct 05, 2006 9:57 pm

triangular numbers
-----------------------------------
For those who don't know what a triangular number is, its a number of the form (n(n+1))/2 where n is a natural number (a number greater than 1)
*6 is a triangular number because it can be written in the form: 
(3(3+1))/2  Now my trouble is finding whats wrong with the code im using to find triangular numbers:

When I look at it, it looks right but theres obviously something missing if it doesnt work correctly  :roll:

-----------------------------------
Ultrahex
Thu Oct 05, 2006 10:16 pm


-----------------------------------
your code looks fine, i think you are just confused in what you did, what you did is it checks for each case it loops through, however IF any of the outputs are true then it is a triangle number.

if you learned functions you could do something like this


function isTriangleNumber (checkNum : int) : boolean
    for x : 1 .. checkNum
        if (x * (x + 1)) / (2) = checkNum then
            result true
        end if
    end for
    result false
end isTriangleNumber

var num : int
put "Enter a natural number: " ..
get num
cls

if isTriangleNumber (num) then
    put num, " is a triangular number"
end if



-----------------------------------
Clayton
Thu Oct 05, 2006 10:16 pm


-----------------------------------
why are you checking to see if (x+(x+1)/2) = num? that doesnt make sense, you ask for a number, shouldnt you be checking for if that number is triangular? or am i completely wrong? (I dont think i quite understand what is supposed to be happening here, so tell me if im wrong)

-----------------------------------
sweetiechic
Thu Oct 05, 2006 10:27 pm


-----------------------------------
im checking if (x+(x+1)/2) = num because if the statement is true, then the number is a triangular number

-----------------------------------
sweetiechic
Thu Oct 05, 2006 10:28 pm


-----------------------------------
actually its not (x+(x+1)/2) = num   
its (x(x+1))/2 = num

-----------------------------------
NikG
Sun Oct 08, 2006 1:26 am


-----------------------------------
why are you checking to see if (x+(x+1)/2) = num? that doesnt make sense, you ask for a number, shouldnt you be checking for if that number is triangular? or am i completely wrong? (I dont think i quite understand what is supposed to be happening here, so tell me if im wrong)You're given num, and you're trying to see if the function holds true for any value of x.  That's why Ultrahex and sweetiechic are counting up from 1 to see if the function is true for any value of x between 1 and num (although this should be num/2).

I'm wondering if it's possible to work backwards... that is given num, try and and solve for x and if x is a whole number, num is a triangular #.

(x(x+1))/2 = num
-> (x(x+1)) = num*2
-> x^2 + x - (num*2) = 0

Now you're left with a standard quadratic....

hmmm... this might require more work so maybe the counter is better.

-----------------------------------
richcash
Sun Oct 08, 2006 1:25 pm


-----------------------------------
(although this should be num/2)
Yeah, at least. Even better, take the the square root of the number and multiply by 2.

(x(x+1))/2 = num 
-> (x(x+1)) = num*2 
-> x^2 + x - (num*2) = 0 
Now you're left with a standard quadratic.... 
Yeah, you're getting somewhere, but doesn't this quadratic only have imaginary numbers if num is a natural number?

-----------------------------------
NikG
Sun Oct 08, 2006 5:30 pm


-----------------------------------
Yeah, you're getting somewhere, but doesn't this quadratic only have imaginary numbers if num is a natural number?I think my math is rusty because I don't know what you just said.

Let's say we test with num=6.
so x^2 + x - 12 = 0 (a=1, b=1, c=-12)
x = (-b +/- sqrt(b^2-4ac))/2
   = (-1 +/- sqrt(1-(-48)))/2
...
doing them separately, x = 3 and -4 (both whole numbers).

essentially, all you have to test is b^2 - 4ac [or 1 + 4(num*2)] because if you cannot take a proper square root of that, your result(s) for x will never be whole.

-----------------------------------
richcash
Sun Oct 08, 2006 6:53 pm


-----------------------------------
Oh, oops, sorry. That's my mistake, somehow I was sure I saw (... + num * 2), lol. :lol: In this case there would only be imaginary roots.

Thanks for pointing out my error, NikG, apparently your math is better than mine. I was getting confused because I knew that x had to have real roots. I like your method a lot, even though for something this easy for loops hardly slow down the program.

-----------------------------------
NikG
Mon Oct 09, 2006 10:08 pm


-----------------------------------
Thanks for pointing out my error, NikG, apparently your math is better than mine.Probably not. I haven't touched math in ages (except for a little trig for my turing games) so I'm lucky I remember anything.

I like your method a lot, even though for something this easy for loops hardly slow down the program.That depends on how large the number you're testing is.
