triangular numbers
Author |
Message |
sweetiechic
|
Posted: Thu Oct 05, 2006 9:57 pm Post subject: 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:
[code]
var num: int
put "Enter a natural number: "..
get num
cls
for x : 1 .. num
if (x * (x + 1)) / (2) = num then
put "e) a triangular number"
else
put "e) not a triangular number"
end if
end for
[/code]
When I look at it, it looks right but theres obviously something missing if it doesnt work correctly |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Ultrahex
|
Posted: Thu Oct 05, 2006 10:16 pm Post subject: (No subject) |
|
|
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
code: |
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
|
Posted: Thu Oct 05, 2006 10:16 pm Post subject: (No subject) |
|
|
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
|
Posted: Thu Oct 05, 2006 10:27 pm Post subject: (No subject) |
|
|
im checking if (x+(x+1)/2) = num because if the statement is true, then the number is a triangular number |
|
|
|
|
|
sweetiechic
|
Posted: Thu Oct 05, 2006 10:28 pm Post subject: (No subject) |
|
|
actually its not (x+(x+1)/2) = num
its (x(x+1))/2 = num |
|
|
|
|
|
NikG
|
Posted: Sun Oct 08, 2006 1:26 am Post subject: (No subject) |
|
|
Freakman wrote: 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
|
Posted: Sun Oct 08, 2006 1:25 pm Post subject: (No subject) |
|
|
NikG wrote: (although this should be num/2)
Yeah, at least. Even better, take the the square root of the number and multiply by 2.
NikG wrote: (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
|
Posted: Sun Oct 08, 2006 5:30 pm Post subject: (No subject) |
|
|
richcash wrote: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
richcash
|
Posted: Sun Oct 08, 2006 6:53 pm Post subject: (No subject) |
|
|
Oh, oops, sorry. That's my mistake, somehow I was sure I saw (... + num * 2), 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
|
Posted: Mon Oct 09, 2006 10:08 pm Post subject: (No subject) |
|
|
richcash wrote: 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.
richcash wrote: 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. |
|
|
|
|
|
|
|