12,824,247 members (44,836 online)

# Algorithms

 Re: Factorials Ian Uy25-Jun-08 7:32 Ian Uy 25-Jun-08 7:32
 Re: Factorials Tim Craig25-Jun-08 9:55 Tim Craig 25-Jun-08 9:55
 Re: Factorials Luc Pattyn25-Jun-08 12:01 Luc Pattyn 25-Jun-08 12:01
 Re: Factorials Tim Craig25-Jun-08 22:06 Tim Craig 25-Jun-08 22:06
 Re: Factorials cp987624-Jun-08 13:33 cp9876 24-Jun-08 13:33
 Re: Factorials Matthew Butler24-Jun-08 13:38 Matthew Butler 24-Jun-08 13:38
 Re: Factorials cp987624-Jun-08 15:05 cp9876 24-Jun-08 15:05
 Re: Factorials Luc Pattyn24-Jun-08 16:02 Luc Pattyn 24-Jun-08 16:02
 Hi Peter,IMO this will behave much better; it gives 4 for 10000000, does not overflow,has no loops but uses a recursion (scaling down by a factor of 5).```private int rightmostNonzeroInFactorial(int n) { int result = 1; // factor ending on 1 // no change // factor ending on 2 int count2 = (n+8) / 10; // factor ending on 3 int count3 = (n + 7) / 10; // factor ending on 4 int count4 = (n + 6) / 10; // factor ending on 5 int count5 = (n + 5) / 10; // factor ending on 6 int count6 = (n + 4) / 10; // factor ending on 7 int count7 = (n + 3) / 10; // factor ending on 8 int count8 = (n + 2) / 10; // factor ending on 9 int count9 = (n + 1) / 10; // factor ending on 0 int count10 = (n + 0) / 10; count5 += count10; if (count5 != 0) { result = (result * rightmostNonzeroInFactorial(count5)) % 10; count2 -= count5; } count2 += 2 * count4; // 4 = 2*2 count2 += 3 * count8; // 8 = 2*2*2 count2 += 4 * count6; // 16 = 2*2*2*2 count3 += 2 * count9; // 9 = 3*3; count3 += 3 * count7; // 27 = 3*3*3 int[] fact3 = new int[] { 1, 3, 9, 7 }; // 3^5 ends on 3 result = (result * fact3[count3 % fact3.Length]) % 10; int[] fact2 = new int[] { 1, 2, 4, 8 }; // 2^5 ends on 2 int factor2=fact2[count2 % fact2.Length]; if (factor2 == 1 && count2 != 0) factor2 = 6; // 2^4 = 16 result = (result * factor2) % 10; return result; } ```Main principle is factors that are not multiples of 5 don't care about theirdigits except the lowest one (and also: there will be more powers of 2 thanpowers of 5). Luc Pattyn [Forum Guidelines] [My Articles]Voting for dummies? No thanks.
 Re: Factorials cp987624-Jun-08 21:09 cp9876 24-Jun-08 21:09
 Re: Factorials Luc Pattyn25-Jun-08 0:53 Luc Pattyn 25-Jun-08 0:53
 Re: Factorials cp987625-Jun-08 3:27 cp9876 25-Jun-08 3:27
 Re: Factorials Luc Pattyn25-Jun-08 3:34 Luc Pattyn 25-Jun-08 3:34
 Re: Factorials cp987625-Jun-08 3:40 cp9876 25-Jun-08 3:40
 Re: Factorials tim_one14-Jul-08 9:37 tim_one 14-Jul-08 9:37
 Re: Factorials Arash Partow25-Jun-08 3:14 Arash Partow 25-Jun-08 3:14
 Re: Factorials Ian Uy25-Jun-08 7:26 Ian Uy 25-Jun-08 7:26
 How to determine the next x,y coordinate for a tank in a 2-D game... Edmundisme18-Jun-08 9:35 Edmundisme 18-Jun-08 9:35
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... 73Zeppelin18-Jun-08 12:38 73Zeppelin 18-Jun-08 12:38
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... Matthew Butler18-Jun-08 12:44 Matthew Butler 18-Jun-08 12:44
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... 73Zeppelin18-Jun-08 23:00 73Zeppelin 18-Jun-08 23:00
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... Edmundisme18-Jun-08 13:00 Edmundisme 18-Jun-08 13:00
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... Matthew Butler18-Jun-08 12:41 Matthew Butler 18-Jun-08 12:41
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... Edmundisme18-Jun-08 13:01 Edmundisme 18-Jun-08 13:01
 Re: How to determine the next x,y coordinate for a tank in a 2-D game... CPallini18-Jun-08 22:19 CPallini 18-Jun-08 22:19
 Polyline offset algorithm beko16-Jun-08 22:59 beko 16-Jun-08 22:59
 Last Visit: 31-Dec-99 19:00     Last Update: 27-Mar-17 18:49 Refresh « Prev1...162163164165166167168169170171 Next »