Project_Euler_20-30

Amicable numbers

题目描述

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where ab, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

题解

def d(n):
    sum = 0
    for i in range(1, n//2 + 1):
        if n % i == 0:
            sum += i
    return sum

result = 0
for i in range(1, 10000):
    t = d(i)
    if i == d(t) and i != t:
        result += i
print(result)

Names scores

题目描述

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

题解


with open('p022_names.txt') as f:
    names = f.read()
names = names.strip('"').split('","')
names = sorted(names)

result = 0

for i in range(len(names)):
    temp_sum = 0
    for j in names[i]:
        temp_sum += (ord(j) - 64)
    result += (temp_sum * (i+1))
print(result)

Non-abundant sums

题目描述

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

题解

自己写了一个,不仅速度慢,而且最后结果也不知道为什么不对。

找到一个网站里面的题解,学到很多。

import math


def abun(N):
    Q = dict.fromkeys(range(1, N+1), 0)
    for q in Q:
        for k in [q * n for n in range(1, int(N / q) +1)]:
            if q != k : Q[k] += q
    return [q for q in Q if Q[q] > q]

N = 28123
A = abun(N)
possible = set()

for a in A:
    for b in A:
        if a+b < N:possible.add(a+b)
        else:break
print(sum([p for p in range(N) if p not in possible]))

## Lexicographic permutations

题目描述

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

### 题解

第一位数是0, 其他任意, 共有 9 ! = 362880 9! = 362880 9!=362880

第一位数是1,其他任意, 共有 9 ! = 362880 9! = 362880 9!=362880

即,  100 ∗ 10000 / 362880 = 2...274240 100 * 10000 / 362880 = 2 ... 274240 10010000/362880=2...274240

那么第一位是2,第二位是1的有, 8 ! = 40320 8! = 40320 8!=40320,

274240 / 40320 = 6...32320 274240 / 40320 = 6...32320 274240/40320=6...32320,即第二位为7(2不取),那么为 27 xxxxxxx

第三位为1的有 7 ! = 5040 7! =5040 7!=5040 ,

32320 / 5040 = 6...2080 32320 / 5040 = 6...2080 32320/5040=6...2080, 即第三位为 8(2,7不取),278xxxxxxx

第四位  6 ! = 720 6!=720 6!=720

2080 / 720 = 2...640 2080 / 720 = 2...640 2080/720=2...640,即第四位为3, 2783xxxxxx

第五位  5 ! = 120 5!= 120 5!=120

640 / 120 = 5...40 640 / 120 =5...40 640/120=5...40, 即第五位为9,  27839xxxx

第六位 4 ! = 24 4! = 24 4!=24

40 / 24 = 1...16 40 / 24 = 1...16 40/24=1...16, 即第六位为1,  278391xxx

第七位 3 ! = 6 3!=6 3!=6

16 / 6 = 2...4 16 / 6 =2...4 16/6=2...4, 即第七位为5, 2783915xxx

第八位 2 ! = 2 2!=2 2!=2,

4 / 2 = 2 4 / 2=2 4/2=2, 此时就出现问题了,余数为0, 那么接下来的三位数就是0,4,6 就是第一位为4的字典序最小的。

…, 然后手动其实就可以求出来,写个程序吧

import math
nums = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
result = 0
N = 100 * 10000
yu = 100 * 10000
for i in range(9, -1, -1):
    idx =yu // math.factorial(i)
    yu = yu % math.factorial(i)
    if yu == 0:
        idx -= 1
    print(nums)
    result += list(nums)[idx] * 10**(i)
    nums.remove(list(nums)[idx])
print(result)

1000-digit Fibonacci number

题目描述

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the index of the first term in the Fibonacci sequence to contain 1000 digits?

### 题解


def f(n):
    F = [0, 1, 1]
    if n < 2:
        return len(str(F[n]))
    else:
        for i in range(n - 2):
            F.append(F[-1] + F[-2])
            del F[0]
        return len(str(F[-1]))

i = 100
while f(i) < 1000:
    if i % 100 == 0:
        print(i)
    i += 1
    
print(f(4782))

Reciprocal cycles

题目描述

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/2=0.5
1/3=0.(3)
1/4=0.25
1/5=0.2
1/6=0.1(6)
1/7=0.(142857)
1/8=0.125
1/9=0.(1)
1/10=0.1

Where 0.1(6) means 0.166666…, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

题解

一个结论,余数在一个循环序列中不会重复出现,故取10 % n, 100 % n,计算出每次的余数,出现相同的就直接返回两个位置差即可。

def Count_Cycle(n):
    yu_dict = {}
    #yu_dict[0] = 1  # 存放余数, 余数:idx
    numiteration = 10
    yu = numiteration % n
    idx = 1
    yu_dict[1] = 0
    while yu != 0 and yu not in yu_dict:
        yu_dict[yu] = idx
        numiteration *= 10
        yu = numiteration % n 
        idx += 1
    if yu == 0:
        return 0
    return idx - yu_dict[yu]

max = 0
idx = 0
for _ in range(1, 1000):
    tmp = Count_Cycle(_)
    if tmp > max:
        max = tmp
        idx = _
print(idx, max)

Quadratic primes

题目描述

题解

import math

def isprime(n):
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True


max_ = 0
result = 0
for a in range(-999, 1000):
    for b in range(-999, 1000):
        k = 0
        tmp = k**2 + a * k + b 
        print(a, b, tmp)
        while tmp > 1 and isprime(tmp) :
            k += 1
            tmp = k**2 + a * k + b
        if k > max_:
            max_ = k
            result = (a,b)
print(max_, result, result[0] * result[1])

Number spiral diagonals

题目描述

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

题解

第一层 1个数

第二层 8个数 = 3 2 − 1 2 = 2 × 4 3^2-1^2 = 2\times4 3212=2×4

第三层 16个数 = 5 2 − 3 2 = 2 × 8 5^2 - 3^2 = 2\times8 5232=2×8

第四层 24个数 = 7 2 − 5 2 = 2 × 12 7^2 - 5^2 = 2\times12 7252=2×12

即第n层 = 2 ∗ ( 2 n − 1 + 2 n − 3 ) = 2 × 4 ( n − 1 ) = 8 ( n − 1 ) 2 * (2n-1 + 2n-3) = 2\times4(n-1) = 8(n - 1) 2(2n1+2n3)=2×4(n1)=8(n1)

对角线的位置为

43444546474849
42212223242526
41207891027
40196121128
39185431229
38171615141330
37363534333231

找规律的题目,第二个函数是自己写的

每一圈对角线上的数求和

def spiral_diag_sum(n):
    if n < 1: return None
    elif n == 1: return 1
    elif n % 2 == 0: return None
    else:
        numbers = [1]
        while len(numbers) < (2*n - 1):
            increment = int(len(numbers) * 0.5 + 1.5)
            for p in range(4):
                numbers.append(numbers[-1] + increment)
             
    return sum(numbers)

def diag_sum(n):
    if n < 1: return None
    elif n == 1: return 1
    elif n % 2 == 0: return None
    else:

        init_ = [3, 5, 7, 9]
        sum_ = sum(init_) + 1
        for i in range(n // 2 - 1):
            for j in range(len(init_)):
                init_[j] = init_[(j - 1) % 4]  + 2 * (i + 2 )
            sum_ += sum(init_)
        return sum_

        
print(diag_sum(1001))
print(spiral_diag_sum(1001))  

Distinct powers

题目描述

Consider all integer combinations of a**b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by a**b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

题解

可能这就是Python吧

result = set()
for a in range(2, 101):
    for b in range(2, 101):
        result.add(a**b)
print(len(result))

Digit fifth powers

题目描述

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

题解

可能这就是Python吧

def get_5_powers_sum(n):
    tmp = str(n)
    result = 0
    for i in tmp:
        result += int(i)**5
    return result

ans = 0
for i in range(2, 1000000):
    if i == get_5_powers_sum(i):
        ans += i
        print(i)

print('ans:', ans)

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

题解

可能这就是Python吧

def get_5_powers_sum(n):
    tmp = str(n)
    result = 0
    for i in tmp:
        result += int(i)**5
    return result

ans = 0
for i in range(2, 1000000):
    if i == get_5_powers_sum(i):
        ans += i
        print(i)

print('ans:', ans)
以下是在openeuler上安装OpenStack的步骤: 1. 确保你的openeuler系统已经安装了必要的软件包和依赖项。可以使用以下命令安装: ```shell sudo dnf install -y python3-devel libffi-devel gcc openssl-devel ``` 2. 添加OpenStack Train软件源。可以使用以下命令添加: ```shell sudo dnf install -y centos-release-openstack-train ``` 3. 安装OpenStack客户端和服务组件。可以使用以下命令安装: ```shell sudo dnf install -y python3-openstackclient openstack-selinux openstack-utils ``` 4. 配置数据库。可以使用以下命令安装MariaDB数据库: ```shell sudo dnf install -y mariadb mariadb-server python3-PyMySQL ``` 然后启动MariaDB服务并设置开机自启: ```shell sudo systemctl enable mariadb.service sudo systemctl start mariadb.service ``` 接下来,使用以下命令来配置MariaDB数据库: ```shell sudo mysql_secure_installation ``` 5. 配置消息队列。可以使用以下命令安装RabbitMQ消息队列: ```shell sudo dnf install -y rabbitmq-server ``` 然后启动RabbitMQ服务并设置开机自启: ```shell sudo systemctl enable rabbitmq-server.service sudo systemctl start rabbitmq-server.service ``` 6. 配置身份认证服务。可以使用以下命令安装Keystone身份认证服务: ```shell sudo dnf install -y openstack-keystone httpd mod_wsgi ``` 然后启动httpd服务并设置开机自启: ```shell sudo systemctl enable httpd.service sudo systemctl start httpd.service ``` 7. 配置计算服务。可以使用以下命令安装Nova计算服务: ```shell sudo dnf install -y openstack-nova-api openstack-nova-conductor \ openstack-nova-console openstack-nova-novncproxy \ openstack-nova-scheduler python3-novaclient ``` 8. 配置网络服务。可以使用以下命令安装Neutron网络服务: ```shell sudo dnf install -y openstack-neutron openstack-neutron-ml2 \ openstack-neutron-linuxbridge ebtables ipset ``` 9. 配置镜像服务。可以使用以下命令安装Glance镜像服务: ```shell sudo dnf install -y openstack-glance ``` 10. 配置块存储服务。可以使用以下命令安装Cinder块存储服务: ```shell sudo dnf install -y openstack-cinder targetcli python-keystone ``` 11. 配置对象存储服务。可以使用以下命令安装Swift对象存储服务: ```shell sudo dnf install -y openstack-swift-proxy python3-swiftclient \ python3-keystoneclient python3-keystonemiddleware \ python3-eventlet xfsprogs rsync ``` 12. 配置Dashboard服务。可以使用以下命令安装Horizon Dashboard服务: ```shell sudo dnf install -y openstack-dashboard ``` 13. 配置OpenStack服务。可以使用以下命令配置OpenStack服务: ```shell sudo openstack-config --set /etc/nova/nova.conf database connection mysql+pymysql://nova:password@controller/nova sudo openstack-config --set /etc/nova/nova.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/nova/nova.conf api auth_strategy keystone sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken project_name service sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken username nova sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken password password sudo openstack-config --set /etc/nova/nova.conf DEFAULT my_ip 10.0.0.11 sudo openstack-config --set /etc/nova/nova.conf DEFAULT use_neutron True sudo openstack-config --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver sudo openstack-config --set /etc/nova/nova.conf vnc enabled true sudo openstack-config --set /etc/nova/nova.conf vnc server_listen 0.0.0.0 sudo openstack-config --set /etc/nova/nova.conf vnc server_proxyclient_address \$my_ip sudo openstack-config --set /etc/nova/nova.conf vnc novncproxy_base_url http://controller:6080/vnc_auto.html sudo openstack-config --set /etc/nova/nova.conf glance api_servers http://controller:9292 sudo openstack-config --set /etc/nova/nova.conf oslo_concurrency lock_path /var/lib/nova/tmp sudo openstack-config --set /etc/neutron/neutron.conf database connection mysql+pymysql://neutron:password@controller/neutron sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT auth_strategy keystone sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken project_name service sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken username neutron sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken password password sudo openstack-config --set /etc/neutron/neutron.conf oslo_concurrency lock_path /var/lib/neutron/tmp sudo openstack-config --set /etc/glance/glance-api.conf database connection mysql+pymysql://glance:password@controller/glance sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_name service sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken username glance sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken password password sudo openstack-config --set /etc/glance/glance-api.conf paste_deploy flavor keystone sudo openstack-config --set /etc/glance/glance-api.conf glance_store stores file,http sudo openstack-config --set /etc/glance/glance-api.conf glance_store default_store file sudo openstack-config --set /etc/glance/glance-api.conf glance_store filesystem_store_datadir /var/lib/glance/images/ sudo openstack-config --set /etc/glance/glance-registry.conf database connection mysql+pymysql://glance:password@controller/glance sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_name service sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken username glance sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken password password sudo openstack-config --set /etc/glance/glance-registry.conf paste_deploy flavor keystone sudo openstack-config --set /etc/cinder/cinder.conf database connection mysql+pymysql://cinder:password@controller/cinder sudo openstack-config --set /etc/cinder/cinder.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/cinder/cinder.conf DEFAULT auth_strategy keystone sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_name service sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken username cinder sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken password password sudo openstack-config --set /etc/cinder/cinder.conf oslo_concurrency lock_path /var/lib/cinder/tmp sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT bind_port 8080 sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT user swift sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT swift_dir /etc/swift sudo openstack-config --set /etc/swift/proxy-server.conf pipeline:main pipeline "catch_errors healthcheck cache authtoken keystoneauth proxy-server" sudo openstack-config --set /etc/swift/proxy-server.conf filter:keystoneauth use "egg:swift#keystoneauth" sudo openstack-config --set /etc/swift/proxy-server.conf filter:keystoneauth operator_roles admin,user sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken paste.filter_factory keystonemiddleware.auth_token:filter_factory sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_uri http://controller:5000 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_type password sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken project_domain_name Default sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken user_domain_name Default sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken project_name service sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken username swift sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken password password sudo openstack-config --set /etc/swift/proxy-server.conf filter:cache use "egg:swift#memcache" sudo openstack-config --set /etc/swift/proxy-server.conf filter:cache memcache_servers controller:11211 sudo openstack-config --set /etc/swift/proxy-server.conf filter:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

involute__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值