Python data structures and algorithms 17th Day "Probabilistic algorithm"

Source: Internet
Author: User

1. Classical probabilities

For example: Mahjong starts to touch the 14 cards without the probability, two identical cards will be, then there are:

All cases: Choose 14 cards from 136 cards for C136-14

No will: The different cards grouped, a total of 34 groups, then take 14 cards, the first time the extraction of c34-1 * 4, the second time for the c33-1 * 4

Total (C34-1 * 4) * (C33-1 * 4) * .... * (C21-1 * 4) = c34-14 * 4^14

Then the probability of no will is c34-14 * 4^14/c136-14

2. From 1!, 2!, 3!, 4!, to n! All the numbers in the first place is 1 probability, the first is the probability of 2, the first is the probability of 3, until the first is the probability of 9

The code is implemented as follows:

#!/usr/bin/env python#! _*_ coding:utf-8 _*_deffirst_number (n):" "First Digit" "     whileN >= 10: N= N/10returnNdefsecond_number (n):" "Second Digit" "     whileN >= 100: N= N/10returnN% 10defthird_number (n):" "Third Digit" "     whileN >= 1000: N= N/10returnN% 100if __name__=="__main__":    #initializes the frequency list, Requency[i] represents the number of occurrences of the first 1frequency = [0 forIinchRange (0, 10, 1)] I= 1#do factorial operations, from 1! , 2! , 3! , until 100! The Operation     forNinchRange (1, 100, 1): I= n *i m=First_number (i) frequency[m]= Frequency[m] + 1PrintFrequency

Results:

/users/liudaoqiang/pycharmprojects/numpy/venv/bin/python/users/liudaoqiang/project/python_project/bat_day17/  7, 7, 7, 3, ten, 4]process finished with exit code 0

3. Ben Ford's Law:

In life, the probability of the first digit being 1 is nearly 1/3, which is 3 times times the 1/9.

Practical application:

(1) factorial, Prime number series, Fibonacci series first

(2) Residential address number,

(3) Economic data anti-fraud, vote anti-fraud

Python data structures and algorithms 17th Day "Probabilistic algorithm"

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.