How much mathematical knowledge does programming need?

Source: Internet
Author: User

Http://sd.csdn.net/a/20120727/2807815.html

Below are some posts I have seen in the Reddit Sub-Forum R/learnprogramming:

    • "How much mathematics does a good programmer need to learn ?"
    • "Should I study mathematics again ?"
    • "This may be the most stupid question I have ever asked. How much mathematics does a good programmer need to learn ?"

There is a misunderstanding between mathematics and programming. Many people think that mathematics must be good or have a high score before learning programming. But how much mathematics does a person need to learn for programming?

In fact, there is no need for many. This articleArticleI will explore in depth the mathematical knowledge required in programming. You may already know.

For basic programming, you need to know the following:

    • Addition, subtraction, multiplication, division-In fact, the computer will help you perform addition, subtraction, multiplication, division operations. You only need to know when to use them.
    • Modulo operation-The modulo operation is used to calculate the remainder. Its symbols are usually expressed by % Percent. So 23 divided by 7 equals 3, and the remainder is 2. 23 mod 7 = 2.
    • Modulo operation for determining whether an odd or even number is used-If you want to know whether a number is an odd or even number, use mod 2 to perform a modulo operation. If the result is 0, it is an even number. If the result is 1, it is an odd number. 23 mod 2 is equal to 1, so 23 is an odd number, and 24 mod 2 is equal to 0, 24 is an even number.
    • Calculates the percentage of a number.That is, multiply the number by a percentage. For example, if you want to get 279 of 54%, 0 is used. 54*279. This means why 1.0 equals 100%, and 0.0 equals 0%.
    • Know what a negative number is.. A negative number multiplied by a negative number is equal to a positive number. A negative number is multiplied by a positive number to a negative number. That's simple.
    • Know the Qadir Coordinate System. In programming, (0, 0) indicates the upper left corner of the screen, and the Y axis is down.
    • Know the law of stock hookBecause it is used to calculate the distance between two points in Cartesian coordinates. Hook law a ^ 2 + ^ 2 = C ^ 2. (X1, Y1) and (X2, Y2) the distance between two points is equal to (x1-x2) ^ 2 + (y1-y2) ^ 2 ).
    • Knowledge of decimal, binary, and hexadecimal. Decimal is the ten digits we usually use: 0-9. We generally think that this decimal system was invented by humans because we have ten fingers.

The computer uses binary data with only two numbers: 0 and 1. This is because computers built with electronic components make it cheaper to recognize only two States (one representing 0 and the other representing 1 ).

The numbers are the same, but the representation in different hexadecimal systems is different, because the numbers in different hexadecimal systems are different. Hexadecimal is six more digits than decimal, so we use a A-F to represent a number greater than 9. The simplest way to express these hexadecimal systems is to use a counter (odometer ). The following three counters display the same number, but in different hexadecimal systems, they have different forms:

View counter page in new window

You don't even need to know how to convert from one hexadecimal system to another. Each typeProgramming LanguageThere are functions to help you convert.

(Note: The hexadecimal format is used because a hexadecimal number can represent four binary numbers. Because the hexadecimal value 3 corresponds to the binary value 0011 and the hexadecimal value a corresponds to the binary value 1010, The 3A (58 in decimal) in the hexadecimal value can be written as the binary value 00111010. Hexadecimal is used in programming because it simplifies binary. No one likes to write all the numbers 0 and 1 .)

That's all. In addition to the hexadecimal system, you can know the mathematical knowledge required for programming. Although it is widely believed that programming requires learning a lot of mathematics, it does not actually need that much. You may writeProgramFor example, an earthquake simulator needs to learn mathematics. In fact, you need to learn the mathematics of earthquakes, rather than the mathematics of earthquake simulators.

More advanced mathematics in some programming Fields

There are some fields that require more mathematical knowledge (but you don't need to know them in 95% of the software .)

    • 3D games and 3D graphics-3D usually involves trigonometric functions and linear algebra (mathematics that uses matrices to solve problems ). Of course, there are many 3D graphics libraries that have implemented these mathematical programming, and you do not need to know these mathematics.
    • 2d Physics (such as Angry Birds) and 3D Physics (such as many popular 3D games)-In order to write Physical Programming, you need to learn some physical equations and formulas (especially mechanics, such as elasticity, gravity, ball rolling and downslope, etc .) However, there are already some physical engines and software libraries for you to implement, so you do not need to know the physical formulas in the game (such as Angry Birds.
    • Encryption-In fact, I mean RSA. You need to know the knowledge about the prime number and how to calculate the maximum common approx.Algorithm, There are many moreProgramming LanguageThere are gcd () functions in it to help you solve the maximum common approx.) Most of the other codes are to move the data according to certain steps. For example, the following flash is the AES "Rijndael" encoding step. All the steps include subtracting some numbers from other numbers, moving the rows up, disturbing the column numbers, and then performing simple addition operations.

If you want to write your own encryption algorithm (usually you do not need to do it, because there are already many good tools, and if you are not an expert in encryption, your program may be easily cracked .) If you only want to encrypt some data, there are already many software libraries for encryption and decryption.

So even in the above cases, you do not need to really know 3D images, physical or encrypted mathematics. You only need to learn to use the software library.

What do programming need to learn?

You need to learn how to model and design algorithms. This means how to abstract the computing or data processing in the real world and writeCodeAnd ask the computer to help you with the computation. For example, in the game Dungeons and Dragons, roles and monsters have many different combat statistics:

    • Hit Points is the damage that a person can experience before his death. The higher the blood point, the more damage it can withstand.
    • The armor class is a measure of your weapon defense capabilities. The lower the defense value, the higher the defense capability of the weapon.
    • Thac0 (read as "thay-Co" and "to hit armor Class 0") is a measure of a person's ability to conduct effective attacks. The lower the value of thac0, the more accurate the attack.
    • The attack power of a weapon is expressed by 1d6 + 2. It indicates the value obtained by shaking a six-sided rolling and then adding 2. 2d4 is to shake two four faces and then add them. ("Dragon and underground city" uses, 12, and 20 shards .)

It depends on the attacker attacking the defender, and let the attacker shake a 20-sided hacker. If the number is greater than or equal to the attacker's thac0 minus the defender's defense capability, the attack will succeed and the defender will be hurt. Otherwise, the defender blocks the attack and does not charge any blood.

Assume that Alice and Bob have the following values:

    • ALICE: HP 14, AC 5, thac0 18, damage 1d6
    • Bob: HP 12, AC 7, thac0 16, damage 2d4

So Alice has more blood points and defensive capabilities (Remember, the lower the AC, the better ). However, Bob is more likely to successfully hit the other party (Remember, the lower the thac0, the better) and cause more damage. We say Bob is more powerful because 2d4 can cause 2-8 points of damage, while Alice 1d6 can only cause 1-6 points of damage. (If you know statistics, you can calculate that Bob's expected damage value is 5, which is higher than Alice's 3.5 .)

You bet Alice or Bob will win the game, right? It's hard to tell who will win. They seem to be evenly matched. Although you may have learned well in statistics, this computation may be a headache. Write the "Dragon and underground city" Program (simulating the Battle Process), you do not even need to know statistics. You only need to run hundreds or thousands of battles to see who wins more.

The following is a program written in Python: (download the code)

  1. Import random, copy
  2. Num_fights=1
  3. Verbose=True
  4. # Lower thac0 and lower AC values are better. Higher damage & HP values are better.
  5. Alicetemplate= {'Name': 'Alice ', 'hp': 14, 'ac': 5, 'thac0': 18, 'dmgnum': 1, 'dmgsize': 6, 'dmgmod': 0}
  6. Bobtemplate= {'Name': 'bob', 'hp ': 12, 'ac': 7, 'thac0': 16, 'dmgnum': 2, 'dmgsize': 4, 'dmgmod': 0}
  7. Def display (s ):
  8. If verbose:
  9. Print (s)
  10. Def attack (attacker, Defender ):
  11. If random. randint (1, 20)>= Attacker ['thac0']-defender ['ac']:
  12. Damage=0
  13. For I in range (attacker ['dmgnum']):
  14. Damage + = random. randint (1, attacker ['dmgsize'])
  15. Damage + = attacker ['dmgmod']
  16. Display ('% s (% s HP) hits % s (% s HP) for % s points of damage. % s is already CED to % s HP. '% (attacker ['name'], attacker ['hp'], defender ['name'], defender ['hp '], damage, defender ['name'], defender ['hp ']-damage ))
  17. Defender ['hp ']-=Damage
  18. Else:
  19. Display ('% s misses % S.' % (attacker ['name'], defender ['name'])
  20. Alicewins=0
  21. Bobwins=0
  22. For I in range (num_fights ):
  23. Display ('======================= ')
  24. Display ('start of combat # % s' % (I + 1 ))
  25. Alice=Copy. Deepcopy (alicetemplate)
  26. Bob=Copy. Deepcopy (bobtemplate)
  27. While true:
  28. Attack (Alice, Bob)
  29. If Bob ['hp ']<= 0:
  30. Break
  31. Attack (Bob, Alice)
  32. If Alice ['hp ']<= 0:
  33. Break
  34. If Alice ['hp ']<= 0:
  35. Display ('Alice has died .')
  36. Bobwins + = 1
  37. If Bob ['hp ']<= 0:
  38. Display ('Bob has died .')
  39. Alicewins + = 1
  40. Print ()
  41. Print ('Alice won % s (% S %) fights. bob won % s (% S %) fights. '% (alicewins, round (alicewins/num_fights * 100, 2), bobwins, round (bobwins/num_fights * 100, 2 )))

When you run this program, you will see:

  1. Start of combat #1
  2. Alice misses Bob.
  3. Bob (12 hp) hits Alice (14 HP) for 6 points of damage. Alice is already CED to 8 hp.
  4. Alice misses Bob.
  5. Bob misses Alice.
  6. Alice misses Bob.
  7. Bob misses Alice.
  8. Alice misses Bob.
  9. Bob misses Alice.
  10. Alice (8 hp) hits Bob (12 hp) for 5 points of damage. Bob is already CED to 7 HP.
  11. Bob misses Alice.
  12. Alice misses Bob.
  13. Bob misses Alice.
  14. Alice misses Bob.
  15. Bob (7 HP) hits Alice (8 hp) for 2 points of damage. Alice is already CED to 6 HP.
  16. Alice (6 HP) hits Bob (7 HP) for 6 points of damage. Bob is already CED to 1 HP.
  17. Bob misses Alice.
  18. Alice (6 HP) hits Bob (1 HP) for 1 points of damage. Bob is already CED to 0 HP.
  19. Bob has died.
  20. Alice won 1 (100.0%) fights. Bob won 0 (0.0%) fights.

But Alice may be lucky in a fight. Let's turn off the output and run the program again (the screen output consumes more time than the running program). When the number of battles reaches 30,000 (Change num_fights to 30000, and change verbose to false ):

    1. Alice wins 12909 (43.03%) battles. Bob wins 17091 (56.97%) battles.

So we can see that Bob takes the lead when using the above value. The computer simulated 30,000 battles. If we use pen, paper, and notebook for 30000 combat simulation, it may take several months to calculate the result, and my notebook only takes 8 seconds.

So what if Alice's blood points increase from 14 to 20. Who will win?

    1. Alice won 19438 (64.79%) battles. Bob won 10562 (35.21%) battles.

We can see that there are 6 more blood points for Alice, and the result is reversed. Alice takes the lead. What if Alice's blood points only increase to 16?

    1. Alice won 15176 (50.59%) battles. Bob won 14824 (49.41%) battles.

Therefore, simply adding two blood points is enough to win Bob's stronger attack strength.

Let's look at this program. It only uses addition, subtraction, multiplication, division to calculate a percentage. Even in more complex programs, when you need to represent magic spells, healing locations, multiple attacks, switching weapons in combat, and other effects, we do not need to know more about mathematics.

Of course, learn more mathematics. Can make you betterProgrammer. But how much mathematics is required to learn programming? Very few.

Update: I think I should add basic algebra to the basic knowledge point, but I only need to know what x * 3 = 12 means that X equals 4.

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.