A few simple digital problems have caused a simple reflection on the computer logic. New posts, experts do not Brick ., Logic

Source: Internet
Author: User

A few simple digital problems have caused a simple reflection on the computer logic. New posts, experts do not Brick ., Logic

Some time ago, before learning how to determine whether a number is a prime number or whether a number is an odd number, I did not think about how computers are implemented. I think the reason is that the thinking of the human brain is very different from the logic of the computer. The thinking of the human brain is three-dimensional, and the logic of the computer is relatively rigid, even though she (Fu said, computer is a big wife, haha) has an advantage in both computing and execution capabilities and accuracy. For example, if an odd number is determined, n is used to calculate the remainder of 2. If the remainder is not 0, it is an odd number. Otherwise, it is not. Yes or not, that is, true or false of the computer's "thinking". In fact, the two are opposite events, and it is easy to use if to determine whether to implement them. In other cases, different categories are also strictly mutually exclusive, which is also if, else if ,...., the theoretical basis of else. For example, a definite number is either a positive number or a negative number or 0.

Speaking of the determination of the prime number, I feel that this thinking is still very tasty. Our logic is to observe that there is a little mathematical foundation. First, we can intuitively see whether or not or even quality factors. The computer is a very dedicated boy. He needs to perform a number-by-number trial in a very rigorous manner, and there are still routines, this generates the logic from 2 to n-1 to remove n (the value to be determined, if a number is found and the first number can be divisible by n, it can be determined that n is a combination number instead of a prime number, it seems that he is still very professional and there is no need to do extra work. If you don't find it, it's the best thing. If you don't find it, click a tag-prime number. In this way, n % I = 0 (I = 2; I <n; I ++) returns a for loop to realize the prime number judgment.

The problem is that we can determine whether it is an odd number or a prime number on the premise that the number is at least an integer, and the prime number must be a positive integer. Whether it is a positive number is actually a good decision. If n is less than or equal to 0, it will be lost directly. So is it an integer that we haven't touched before? How can we judge it?

I thought a lot of ways and I always felt a little worse. At the beginning of programming, I didn't really understand the logic of the computer, and I couldn't put a command or an idea that I could understand into the computer. Let's look back at the implementation process of prime numbers and odd numbers. In fact, the computer is very naive. Let's look at the following statement n * 10/10 = 0, my gold. Isn't n an integer. This is not something that the human brain can think of in programming logic. Next, let's look at the presentation process of a digital problem.

Evaluate the values of the following expressions and write out one or more implementation methods: 1-2 + 3-4 + ...... + N.

The Code introduction is meaningful. It is interesting to think about it carefully. It is feasible to test it by yourself (the output computation code is not written, nor is it the focus of this Article ). Of course, there are many methods to achieve this. We may use our mathematical knowledge to analyze and calculate this series first. It is not difficult to conclude that when n is an even number, and sum =-n/2; when n is an odd number, sum = (n + 1)/2. Let's take a look at the implementation code and running results:

It is also feasible. Next, let's compare the two ideas. Which one is better?

Obviously, the second approach is closer to an analytical method of our human brain, which makes this method very violent for programming, in the process of implementation, the computer only plays the role of output, that is, display, and does not exert its powerful computing capabilities, because the real computing and thinking we have already done. So next we should be more inclined to the logic of the first method, and try to solve practical problems with computer "thinking.

This post is right and written here. For more questions about numbers, more examples will be provided in the next article, which is also interesting.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.