当前位置:网站首页>Sum of squares of each bit of a number
Sum of squares of each bit of a number
2022-06-28 04:27:00 【Beauty of algorithm and programming】
introduction
It may be a simple thing to add the squares of each bit of a number in a mathematical problem , But after turning this problem into a programming problem ? For example, this number has many digits ? And what if this number is not just the square of each bit , It's going on 10 Inferior or 20 Power , Or how to solve this problem to a higher power ?problem
There is a function f(n), Please calculate by f(n)=n The sum of squares of each bit of . for example
f (13) = 1^2+3^2, f (207) = 2^2+0^2+7^2Method
First set a function , From the keyboard n The numerical , Because I want to add the squares of each bit , So you can use a loop , yes sum=0 Reuse sum Add the sum of the squares of each bit , Back again sum Got it. f(n) Value . Or you can use a list , Using a loop , Add the square step by step to get f(n) Value .
Experimental results and discussion
Through the experiment 、 Practice has proved that the proposed method is effective , Is able to solve the problem raised at the beginning .
Code list 1(1)
def f(n):
n=input(‘ Please enter a positive integer :’)
sum=0
for x in n:
x=int(n)**2
sum=sum + x
return sum
print(f(n))
(2)
def f(n):
n=input(‘ Please enter a positive integer :’)
sum=0
while n>0:
s= len(str(n))
z=int(str(n)[s-1]) ** 2
s=s-1
sum=sum + z
if s<0:
break
return sum
print(f(n))Conclusion
Sometimes using mathematical thinking can only solve a small number of problems , When the data becomes large, it will become very complicated to use mathematical thinking , When using mathematical thinking, I will express the numerical value in the program as its single digit , ten , Hundred bit . If the value is tens of thousands , How to calculate hundreds of millions , This is the need to express it in the program , It's easy to think of values as a list , It can easily solve large numerical problems .
边栏推荐
- Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal
- Notes to friendship chain
- 2021年终总结及2022年展望
- A queue of two stacks
- Database garbled
- 由两个栈组成的队列
- Introversion, lying flat and midlife crisis
- Why are cloud vendors targeting this KPI?
- leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
- Introduction notes to machine learning
猜你喜欢

La norme européenne en 597 - 1 pour les meubles est - elle la même que les deux normes en 597 - 2 pour les ignifuges?

first. Net core MVC project

Little knowledge about function templates --

With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan

Recommended by Alibaba P8, Fiddler packet capturing tool (I)

Visualization of loss using tensorboard

有关函数模板的那些小知识-.-

Why are cloud vendors targeting this KPI?

Notes to friendship chain

From zero to one, I will teach you to build a "search by text and map" search service (I)
随机推荐
Aspnetcoreratelimit rate limit interface access limit current limit control
【Proteus仿真】定时器1外部计数中断
2021年终总结及2022年展望
从零到一,教你搭建「以文搜图」搜索服务(一)
Little knowledge about function templates --
After launching the MES system, these changes have taken place in the enterprise
《性能之巅第2版》阅读笔记(二)--CPU监测
Has any boss ever seen repeated binlog messages when MySQL CDC uses datastream
Analyse complète annuelle du marché chinois de l'audio en 2022
04 summary of various query operations and aggregation operations of mongodb
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
PostgreSQL 实现批量更新、删除、插入
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
Genicam gentl standard ver1.5 (2)
Design a stack with getmin function
Reverse a stack with recursive functions and stack operations only
请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
mysql导入文本文件时的pager
RT thread bidirectional linked list (learning notes)
【MySQL】多表连接查询