4

I'm currently working on a project that I'm working on, and I'm currently learning about looping. Here is the direction as follows...

Triangular numbers are numbers of objects that could be arranged in a triangle by making rows, with one more object in each row than in the previous row. Write a function that given a number, n, will formulaically compute the nth Triangular number. Write another function that displays the Triangular numbers up to and including n.

The formula states (n(n+1))/(2) or (n^(2)+n)/(2)

So pretty much I think I would need to formulate a function that whatever I enter for n to the equation I would get the answer. However, my question is I don't understand how loops is used in this scenario. I have done the following but I'm getting an error. I think it should very simple right?

n=int(input("Please Enter n:"))
y1=((n**2)+n)/(2)   
print (y1)

I think the code above answers the first question where it formulaically compute the nth Triangular number, given inputing n. However, I'm having a hard time to write a function for the second question where a function that displays the Triangular numbers up to and including n. Thank you very much for your help.

2 Answers 2

2

Could be something like this:

def all_triangle_numbers(n):
    for i in range(1, n + 1):   
        print("n = {0}, triangle = {1}".format(i, (i ** 2 + i)//2))

all_triangle_numbers(10)        
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you. Would you be able to help me for the second question? I'm having a hard time to write a function for the second question where a function that displays the Triangular numbers up to and including n.(Which I don't get quite what it means in the first place).
Thank you for your work. But, I'm having trouble because it asks up to and including n. How does that work?
I'm using an online complier, but it does work. I see from 1 to 10.
Glad I helped, enjoy!
0

You are probably getting an error because of your input(), which should be raw_input(). However, if that is not the case, please state exactly what error you are getting.

Here is some working code:

def triangle(n):
    return ((n**2)+2)/2.0

n = int(raw_input('Please enter an integer: '))
print triangle(n)

Or, to print all the triangle numbers up to and including n:

def all_triangles(n):
    for i in range(1, n+1):
        print ((i**2)+2)/2.0,

n = int(raw_input('Please enter an integer: '))
all_triangles(n)

3 Comments

I think the code actually works, but I will try out yours as well. Would you be able to help me for the second question? I'm having a hard time to write a function for the second question where a function that displays the Triangular numbers up to and including n.(Which I don't get quite what it means in the first place).
@python2learn, it sounds like this is the original first question that you had. This is the part that it is asking for either a for loop or a while loop. If this were me, I would make a while loop that states: while (n>=0){#stuff; --n}. Maybe this can implemented into the above answer?
Sorry I'm having trouble grasping on it. Would you be able to show me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.