Python Forum
permutations calculation with a while instruction
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
permutations calculation with a while instruction
#1
Hi,

I wrote this code

x=input('donnez un nombre')
y=int(x)
d=y
while d>=1:
	y=y*(y-1)
	d=d-1
print(y)
but for 4 in input cmd give me 298995972 instead of 24..

why?

Best regards,
Reply
#2
You can debug your code by adding print('Current state:', y, y-1) after while loop declaration.
If you do that, you will see the output:

Output:
4 3 12 11 132 131 17292 17291 298995972 298995971
Tip: the counter, variable d, does not affect on variable y that accumulates the result. So, you need to do some changes in line 5 and probably line 3 of your code.
Reply
#3
Thank you,

Here a code which seem ok

x=input('donnez un nombre')
y=int(x)
a=1
while y>=1:
	a=a*y
	print('Current state:', y, y-1)
	y=y-1
print(a)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Illegal instruction (core dumped) when import pandas. amandacstr 1 2,106 Dec-31-2022, 11:31 PM
Last Post: stevendaprano
  Creating permutations of N size with no same elements next to one another (recursion) melmoman 1 1,555 Dec-18-2021, 09:39 PM
Last Post: Larz60+
  Illegal instruction? working code for months? korenron 4 13,101 Aug-05-2021, 09:57 AM
Last Post: korenron
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,401 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Permutations mikke3141 2 20,242 Dec-23-2019, 06:09 PM
Last Post: mikke3141
  More Efficent Way of Generating Permutations/ w Rep ClassicalSoul 2 2,698 Aug-22-2019, 05:22 AM
Last Post: perfringo
  How to find the number of permutations of a list of lists? JoeB 5 5,346 Nov-18-2017, 06:31 PM
Last Post: heiner55
  instruction refused on a "while loop" sylas 11 7,860 May-22-2017, 08:09 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020