

Ian Uy wrote: Yup, for some reason it did threw the right answers.
I think if you're aiming for the ACM ICPC then you should try to understand why it gives the correct answers (and at first glance it looks as if it should). The fact that i may not be prime actually doesn't matter (well except for efficiency concerns).
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





Hi,
one remark: you should replace the if(){} construct by a while(){} and drop the i;
that way your code is more readable, and you can now more easily reduce the divisor candidates
(say to only 2 and odd numbers, or just primes, or whatever else has already been suggested).





Robert.C.Cartaino wrote: As a college student, you really should be able to do this. It's basic math.
To be fair, my bet is 99% of college students don't know (or care) how to do this, along with 99% of the rest of the world.
I'm about to finish up my second engineering degree, and prime numbers have not been brought up once during the 5 years i've been here.





MarkBrock wrote: my bet is 99% of college students don't know (or care) how to do this, along with 99% of the rest of the world.
I agree. Maybe a bit more than 99%, like 99.9999%
"The clue train passed his station without stopping."  John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks"  Pete O'Hanlon





MarkBrock wrote: To be fair, my bet is 99% of college students don't know (or care) how to do this, along with 99% of the rest of the world.
You must have been taught how at junior school, though
I know, since my son has this in his Math book.





ChandraRam wrote: You must have been taught how at junior school, though
Yeah, I was.
Sit down and watch the TV show "Are you smarter than a 5th grader". Unless you've got better memory than an elephant, I bet you can't answer most of the questions .





You do not need to divide by all numbers, only by primes. If the number is evenly divided by a prime, then keep dividing by that prime until the result leaves a remainder, then go to the next prime. There exists (google primes) a site on which there is a list of the primes for the first million numbers, the second million numbers ... to the first 15 million numbers.
Dave Augustine.





Member 4194593 wrote: You do not need to divide by all numbers, only by primes.
... and, if we are optimizing, your forloop does not need to run all the way up to InputCase . You only need to check for divisibility of numbers up to the square root of InputCase .





I see. I'll apply both suggestions.
That's why its so slow at 20!.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Ian Uy wrote: That's why its so slow at 20!.
There are easier ways of factorizing 20!
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





A possible solution would be as follows:
void prime_factors(unsigned int n, std::deque<std::pair<unsigned int,unsigned int>>& factor_list)
{
factor_list.clear();
unsigned int upper_bound = ::floor(std::sqrt(n));
unsigned int i = 2;
while(i <= upper_bound)
{
std::pair<unsigned int, unsigned int> current_factor(i,0);
while(0 == (n % i))
{
n /= i;
++current_factor.second;
}
if (current_factor.second > 0)
{
factor_list.push_back(current_factor);
}
++i;
}
}
The primefactors will be in the deque, the first of each element is the factor and the second is the recurrence count of the factor.
[updated]





[edit] since Arash has completely changed the code in his previous post, this appears somewhat out of context [/edit]
I think this is an example of misguided optimization. Whilst Ian Uy's example looks inefficient
int main()
{
int InputCase;
cin >> InputCase;
for(int i=2;i<=InputCase;i++)
{
if(InputCase%i==0)
{
cout << i;
InputCase/=i;
i;
}
}
return 0;
}
all he is doing each iteration is testing
if(InputCase%i==0)
In order to avoid these "unnecessary" tests, you are testing each n as
if (n % 2 == 0) n++;
while(!is_prime(n)) { ++n; }
The function is_prime(n) will involve much more overhead than the original code, and it doesn't matter that 'i' may not be prime.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
modified on Saturday, October 18, 2008 11:35 PM





Hi Peter,
I agree.
Some improvements are possible, but they do not involve adding method calls:
1. replacing if(){} by while(){} and dropping the i;
then changing the for loop so it starts with 3 and increments by 2 or by 2 and 4 alternating
(make sure to test 2 itself also)
2. use multiple (say two) threads, assuming a Core Duo or something similar.





Hi Luc,
True. The most important thing he should do is limit the testing to sqrt(N).
To factorise a number N, you need to test up to sqrt(N), and there are approximately
sqrt(N)/log(sqrt(N)) primes to test. For example, to factorise 1,000,000 you only need to test the 168 primes less than 1000, or 16.8% of the numbers less than 1000.
Using the simple algorithm you test 100% of the numbers less than sqrt(N):
if you eliminate the multiples of 2 you are down to testing about 50%
then eliminating the multiples of 3 drops you to testing 33.3%
then eliminating the multiples of 5 drops you to testing 26.7%
and so on.
the first few are probably worthwhile as you point out, but you then rapidly get in to diminishing returns, and may easily get to the stage where it is not worth the effort to save the time of the line
if(InputCase%i==0)
I don't think multiple threads are worthwhile until you go past 32 bit arithmetic (even the simplest algorithm only has 2^16 tests to do).
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
modified on Sunday, June 29, 2008 10:10 PM





cp9876 wrote: The most important thing he should do is limit the testing to sqrt(N).
I believe this is not correct, assume you have an x composed from 2p where p is a prime, in this case it is clear that in certain circumstance sqrt(x) can be less than p (eg: 15838 = 7919 * 2, where 7919 > sqrt(15838)), for primal testing the sqrt(n) bound is ok, however its not suitable for the upper bound in this problem.





Arash Partow wrote: I believe this is not correct, assume you have an x composed from 2p where p is a prime, in this case it is clear that in certain circumstance sqrt(x) can be less than p (eg: 15838 = 7919 * 2, where 7919 > sqrt(15838)), for primal testing the sqrt(n) bound is ok, however its not suitable for the upper bound in this problem.
He's studying for the ACM competition  I don't want to spoon feed him! He can limit his testing to sqrt(N) but he has to modify his algorithm slightly for when InputCase is not 1 at the end.
In the example you give, he only needs to test until floor(sqrt(15838))=125, he will have only found 2 as a factor and InputCase will be 7919 after the 124 tests. He can then conclude the the remaining value in InputCase is the remaining prime factor. This is much more efficient than testing the remaining 15,703 factors.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





cp9876 wrote: This is much more efficient than testing the remaining 15,703 factors.
true.





you're right, as long as one divides by the current number until the remainder is nonzero they will have taken out all of the multipleoffactors as well, no need to explicitly only test/divide by primes.
good catch!





hi
can anyone tell me how the vale is calculated using prob function of ms excel
it is used like this:
PROB
Returns the probability that values in a range are between two limits. If upper_limit is not supplied, returns the probability that values in x_range are equal to lower_limit.
Syntax
PROB(x_range,prob_range,lower_limit,upper_limit)
X_range is the range of numeric values of x with which there are associated probabilities.
Prob_range is a set of probabilities associated with values in x_range.
Lower_limit is the lower bound on the value for which you want a probability.
Upper_limit is the optional upper bound on the value for which you want a probability.
Remarks
If any value in prob_range = 0 or if any value in prob_range > 1, PROB returns the #NUM! error value.
If the sum of the values in prob_range ¹ 1, PROB returns the #NUM! error value.
If upper_limit is omitted, PROB returns the probability of being equal to lower_limit.
If x_range and prob_range contain a different number of data points, PROB returns the #N/A error value





Is it possible to get the rightmost NONZERO digit of a factorial without calculating for it?
Say 1000! = 2
Regards,
Ian
*NO, this is not a homework.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





I assume you mean without calculating the factorial but some simpler, faster calculation? You can rewrite a factorial as a polynomial in n but I'm not sure if that gives you an easy pattern for the last digit. And by definition, all factorials except 1! and 0! are even. Does this have a practical application or are you just curious?
If you don't have the data, you're just another a**hole with an opinion.





Nope. This is part of the question set my adviser gave me to prepare for a certain competition.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Ian Uy wrote: It is said that the most complex structures built by mankind are software systems.
I've heard that bandied about but I think I'll disagree. They usually quote Vista at 50 million lines of code yet the latest generation of processor chips boast over 2 billion transistors. And you could argue that much of what gets counted in an operating system are simply utilities that are applications running on the base system so the core is smaller still.
If you don't have the data, you're just another a**hole with an opinion.





Hi,
I tend to disagree. Hardware, especially CPU design, mainly consists of very repetitive
structures, whereas software, all kinds of it, is very inhomogeneous. And a new CPU gets
designed in a matter of months, a new OS takes several years.
That helps explaining why you would find less than 10 bugs in your CPU having
2 billion transistors, and several thousands of bugs in just 50 million lines of Windows code.
So if you want to compare complexity, I suggest you look at design effort, and number
of bugs.





I still will disagree. In programming we keep using the same structures over and over again but insist on lovingly hand coding them each time. The chip makers have better tools and love to reuse their building blocks. A large part of their reuse is automated which explains the smaller number of bugs. They prove their components and then build up. Programmers make the same mistakes at ever increasing scales.
On another note, humans make machines with millions of parts. I will contend that one line of code is not equivalent to designing one mechanical part in complexity.
If you don't have the data, you're just another a**hole with an opinion.





Ian Uy wrote: Is it possible to get the rightmost NONZERO digit of a factorial without calculating for it?
Yes, for example the last nonzero digit of 1,000,000! is 4.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





Got it!... after about 10 mins of tinkering...
static int fac_last_digit(int x)
{
int res = x;
int mod = 10;
while (x > 1)
{
if ((res * (x  1)) % mod == 0) mod *= 10;
res = (res * (x)) % mod;
}
return (res / (mod / 10));
}
The trick is to use the % operator with increasing powers of 10 depending on if the result is a multiple of the original modulus value...
(Needs tidying up a little)... why you need this is a mystery though.
Matthew Butler





Nice try, but you will quickly fall over without a large integer class. Even 1000! ends with 249 zeros, so mod will grow to about 10^249 or so.
If you get bored, try to write a routine to compute the last nonzero digit of googol! = (10^100)!, you should get 6.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





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;
int count2 = (n+8) / 10;
int count3 = (n + 7) / 10;
int count4 = (n + 6) / 10;
int count5 = (n + 5) / 10;
int count6 = (n + 4) / 10;
int count7 = (n + 3) / 10;
int count8 = (n + 2) / 10;
int count9 = (n + 1) / 10;
int count10 = (n + 0) / 10;
count5 += count10;
if (count5 != 0) {
result = (result * rightmostNonzeroInFactorial(count5)) % 10;
count2 = count5;
}
count2 += 2 * count4; count2 += 3 * count8; count2 += 4 * count6;
count3 += 2 * count9; count3 += 3 * count7;
int[] fact3 = new int[] { 1, 3, 9, 7 }; result = (result * fact3[count3 % fact3.Length]) % 10;
int[] fact2 = new int[] { 1, 2, 4, 8 }; int factor2=fact2[count2 % fact2.Length];
if (factor2 == 1 && count2 != 0) factor2 = 6; result = (result * factor2) % 10;
return result;
}
Main principle is factors that are not multiples of 5 don't care about their
digits except the lowest one (and also: there will be more powers of 2 than
powers of 5).





Hi Luc,
I haven't worked through your code (are you sure the countN's do the right thing?).
There is a little known fact (don't ask me how I know it!) that
if n = 5q+r with 0 <= r < 5
then n! has the same rightmost nonzero digit as 2^n x q! x r!
Using this the problems reduce easily.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





Hi Peter,
1.
countN tells me how many factors of n! end on digit N
2.
the recursion takes care of your q!
3.
I ran a check for all [1,500] and for 10000000
regards,





Luc Pattyn wrote: countN tells me how many factors of n! end on digit N
OK now I see what you are doing. I'm sure you have some very clever reason for this, but it is not clear to me why you do
count5 += count10;
if (count5 != 0)
{
result = (result * rightmostNonzeroInFactorial(count5)) % 10;
count2 = count5;
}
and not what I would expect:
if (count5 != 0)
{
result = (result * rightmostNonzeroInFactorial(count5)) % 10;
count2 = count5;
}
if (count10 != 0)
{
result = (result * rightmostNonzeroInFactorial(count10)) % 10;
}
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





Hi Peter,
the way you propose the factors that end on 5 don't get represented by count5 factorial
(they only need odd multiples of 5), hence the recursion would not be correct.
I expect the theory that explains my code would be the same as the one that led to the
formula you've mentioned before.





Ah now I see (it's late here
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





cp9876 wrote: ...There is a little known fact (don't ask me how I know it!) that
if n = 5q+r with 0 <= r < 5
then n! has the same rightmost nonzero digit as 2^n x q! x r!
That should be 2^q rather than 2^n, and then it's valid for all n > 4. Removing recursion, here's one way to write it (in Python):
def f(n):
result = 1
while n > 1:
n, r = divmod(n, 5)
result = result * (6, 2, 4, 8)[n & 3] * (1, 1, 2, 6, 4)[r] % 10
return result
and that can be simplified a bit by using a larger table:
FACTAB = 6, 6, 2, 6, 4, 2, 2, 4, 2, 8, 4, 4, 8, 4, 6, 8, 8, 6, 8, 2
def f(n):
result = 1
while n > 1:
result = result * FACTAB[n % 20] % 10
n return result





I believe this to be an ACM question from either 96' or 97'
bool nonzerodigit(const unsigned int& n, unsigned int& result)
{
if (n > 100000) return false;
unsigned int result = 1;
for (unsigned int i = 1; i <= n; ++i)
{
unsigned int f = i;
while (0 == (f % 5))
{
f /= 5;
result /= 2;
}
result = (result % 100000) * f;
}
result %= 10;
return true;
}
100000 is an upper sanity bound and also a quantizer for the minimum required 6 "least significant" digits.





Thank you!
I am training for the next ACM ICPC Regionals.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Hello,
I'm putting together a Silverlight 2 demo application. It is going to be a simple 2D tank game where you can use the arrow keys to rotate and move the tank. The tank can point in any direction (360 possible directions, right?). I've written the code to rotate the tank, but I'm not sure where to begin regarding moving the tank. How can I take the angle of the tank (say, 156 degrees for example) and use that to determine the next x,y position of the tank when it moves forward?
I'm sure there must be a simple equation for this, but I don't know what it would be called or even how to google this problem.
Any ideas?
Thanks,
Ian





Define the motion using a vector. The position coordinates are given by:
x = r*cos(theta)
y = r*sin(theta)
where r is the distance traveled and theta is the angle. You can get more description here[^]. You'll also have to assign a velocity.
I'm the ocean. I'm a giant undertow.





Beat me to it.
(Within 3 minutes, more than 3 hours from the original question, what are the chances of that?)
Matthew Butler





Pretty low I would imagine....wacky!
I'm the ocean. I'm a giant undertow.






Just use basic geometry/maths...
nextXpos = lastXpos + speed * sin(angle);
nextYpos = lastYpos  speed * cos(angle);
[angle is measured from the vertical, clockwise]
The only things you need to remember are:
> Math.Sin and Math.Cos take the angle argument in radians not degrees.
> Sin and Cos return double precision numbers: which may round to 0 or 1 if you are using integer positions and therefore the tank will not move as you want.
Hope this helps.
Matthew Butler






Edmundisme wrote: Thanks!
Well, should be tanks, instead...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
 Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
 Iain Clarke





Hello,
i have set of 2D points and i want to offset with a given distance (like offset command in AutoCAD)
i do not know how to deal with corners. i have searched on Internet, there are advanced methods like straight skeletons etc. but my polyline is not self crossing and no holes in it.
Is there any simple method? any references?
Best Regards.
Bekir





Why can't you just iterate through the points and add your offset to each one? It seems like this should translate the corners along with everything else.






beko wrote: Just translating the edges with the offset distance will not work.
Why not?
beko wrote: I have found an algorithm not tested throughly, but it is working at least for my very simple 3 points polyline test
What kind of test you did?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
 Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
 Iain Clarke





Hello,
At the end, it is a translation however it must be implemented with respect to the normals, as far as i read through if it is a parametric curve then it is rather complicated. If you like to see how much it can further get complicated, you can have a look at the link below.
http://timeguy.com/cradekfiles/emc/liu2007_line_arc_offset.pdf[^]
for the test, i have just created a polyline with three points and used the algorithm from Eduardo in my earlier reply, and compared it with AutoCAD output
Best Regards.




