-4

Can anyone help with this, I'm trying it on Python. So, theres a task that says

Write a simple program that prompts the user for a certain number of cities for the Traveling Salesman problem, and displays the total number of possible routes that can be taken. Program should look like this: ''How many cities? __'' (first line) and ''For __ cities, there are __ possible routes.

I think I've done right the first part:

x = int(input("How many cities? "))

but stuck with the second one. I think about using if, wonder if there is a way to make something like this on python: (x-1)*(x-1) - while the second x is the x-1 of first. Hope you get it :) Any suggestions?

bereal
  • 32,519
  • 6
  • 58
  • 104
  • Paste your whole code, so we can help you – user1767754 Nov 30 '14 at 14:35
  • Does the assignment assume that each city is connected to each? – bereal Nov 30 '14 at 14:37
  • what I have I already pasted, I just not sure what to use there cause I'm just a fish in programming at all. Just need to find out how many ways of traveling existing. For example 10 cities has 3628800 routes (9*8*7*6*5*4*3*2) – Valerius Bokštas Nov 30 '14 at 14:40
  • If cities are not connected, there are no routes. The answer is 0, problem is solved. If seriously, what you're looking for, is called [factorial](http://en.wikipedia.org/wiki/Factorial). See [math.factorial](https://docs.python.org/2/library/math.html#math.factorial), but preferably (for educational purposes), use a `for` loop. – bereal Nov 30 '14 at 14:41
  • so, this should help me then? http://stackoverflow.com/questions/5136447/function-for-factorial-in-python – Valerius Bokštas Nov 30 '14 at 14:44
  • Yes, exactly. Also, [here](https://gist.github.com/fmeyer/289467) you'll find quite a few other ways. – bereal Nov 30 '14 at 14:47

1 Answers1

0

If anyone will search answer, I did it!

import math
x = int(input("How many cities? "))
a = math.factorial(x)
print("For", x, "cities, there are", a, "possible routes")
bereal
  • 32,519
  • 6
  • 58
  • 104