"Algorithm 5" to find the mean value of the sum of the digits of the different binary representations

Source: Internet
Author: User
Tags gcd greatest common divisor

Despite being a CS major student, Little B has a very good mathematical foundation and a special interest in numerical computing, and likes to use computer programs to solve mathematical problems, and now she is playing a game of numerical transformation. She found that computers often use different binary representation of a number, such as the decimal number 123 is expressed as 16 binary only contains two digits 7, one (B), with octal representation of three digits 1, 7, 3, according to the different binary expression, each single digit and also different, In the example above, the numbers in hexadecimal and octal are 18 and 11, respectively. Small B is interested in, a number a if the 2 to A-1 in the expression, the sum of the average number of single digits? She wants you to help her solve the problem? All calculations are based on decimal, and the result is expressed in decimal as a fractional form of irreducible simplicity.

1 #Coding=utf-82 3 defgcd (x, y):4     ifx>=y:5Ma=x6Mi=y7     Else:8Ma=y9Mi=xTen  One     ifma%mi==0: A         returnmi -     Else: -         returnGCD (mi,ma%mi) the  -A=Int (raw_input ()) -sum=0 -  forIinchRange (2, a): +m=a -      while(m>0): +r=m%I Am=m/I atSum + =R -  - PrintSTR (SUM/GCD (sum,a-2)) +'/'+str ((a-2)/GCD (sum,a-2))

The 18-23-line code solves the sum of the digits in the number a represented as 2 into the A-1 binary

Because the topic requires the result to be reduced to fractions, so the numerator is sum, the denominator is a-2, need to require their two greatest common divisor (the division method to achieve, see 3-14 lines), and then the numerator denominator divided by greatest common divisor, the final printing with a string connection form, '/' The numerator and denominator are connected together to output.

"Algorithm 5" to find the mean value of the sum of the digits of the different binary representations

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.