You've reduced the problem from being O n to being O 1. That's a really nice speed-up! The problem is that you haven't done what the client teacher has asked. While I would argue that your solution is objectively better, it doesn't do one thing that was required use a loop and does one thing that wasn't specified gets the input value from the user.
I don't know your teacher, so I don't know if they'll care, but I have dealt with clients that had very specific requirements and it usually is a good idea to follow them and discuss with them why another solution would be better before implementing it. Presumably your teacher wanted you to use a loop because they wanted you to have practice writing and using loops! Also, if you are going to get input from the user, you should validate it.
To avoid this, we can divide the multiplicands by 6 before multiplying them together. We can infer that exactly one of the three is a multiple of 3; similarly, one of the first two must be a multiple of 2.
So we need to divide one value by 3, and also divide one value by 2 this will be the same value, if n is a multiple of 6 :. I've also increased the range to unsigned long int , which may have a bigger range, depending on your target platform. This is a good lesson in taking results from pure mathematics and expressing them in a program: real-world mathematical numbers don't have the same limitations on precision and range that computer numbers do, and we often have to adapt our algorithms to work well with their machine representations.
BTW, this approach is probably not acceptable for your homework if it prescribes that you must use a loop for the calculation, but it is a valuable learning opportunity, so it's good that you explore this other approach and discover something your teacher hasn't put in front of you; I find that the most effective way to learn, and perhaps you do too.
Happy programming! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Calculating sum of squares in C Ask Question.
Asked 4 years, 4 months ago. Active 4 years, 4 months ago. Viewed 1k times. Is it more efficient than loops? Improve this question. Vogel Ask your boss why he needs you to use for loops when a while would be easier to understand.
Maybe there's an old macro that causes while loops to bug out, and the temporary work around is to use a for. I wish that wasn't a true story. Check Whether a Number is Prime or Not. Check Whether a Number is Palindrome or Not. Generate Multiplication Table. Calculate Average Using Arrays. C Program to Calculate the Sum of Natural Numbers In this example, you will learn to calculate the sum of natural numbers entered by the user.
To understand this example, you should have the knowledge of the following C programming topics: C for Loop C while and do Share on:. Did you find this article helpful? Sorry about that. How can we improve it? Leave this field blank.
Related Examples. C Example Display Factors of a Number. Get App Get C App. For every number x, we recur for n-x. Below is the recursive formula. Below is a simple recursive solution based on the above recursive formula.
If we draw the recursion tree, we can observe that many subproblems are solved again and again. So the subproblem for 4 is called twice. Since the same subproblems are called again, this problem has the Overlapping Subproblems property. So min square sum problem has both properties see this and this of a dynamic programming problem.
Like other typical Dynamic Programming DP problems , recomputations of the same subproblems can be avoided by constructing a temporary array table[][] in a bottom-up manner. Below is a Dynamic programming-based solution. Ceiling Math. Here is the basic idea of how it can be done.
So, for every node, we will push the next possible valid path which is not visited yet into a queue and, and if it reaches node 0, we will update our answer if it is less than the answer.
Collections; using System. Min ans, p. Enqueue new Point path, p.
0コメント