Python 计算当真因子个数为偶数个时为幸运数,计算区间内幸运数之和
晚饭后朋友发来个问题,正好无事做,动手写了一下
若一个正整数有偶数个不同的真因子,则称该数为幸运数。如4含有2个真因子为 1 和 2 。故4是幸运数。求【2,100】之间的全部幸运数之和。
常规思路
被除数一直除以 1 2 3 ... 直到除以它自身,不过这种比较消耗资源(周知python简洁但效率不高)
getf.py
def get_Factor(x):
"""
n 需要求真因数的数(被除数) x
x 除数 y
rem 余数
quo 商
"""
if x == 0: return [0]
if x == 1: return [1]
f_list = []
for y in range(1,x):#定义y是除数
rem = x % y
quo = x // y
if rem == 0:# 如果x可以被y整除
if y not in f_list:
f_list.append(y)
if quo not in f_list:
f_list.append(quo)
continue
continue
continue f_list.sort(reverse = False)
f_list.pop()
#是一个一个加进去,排个序后删除本身
return x , f_list def get_Luckynum(a, b):
Luckynum = []
for i in range(a,b+1):
i,f_list = get_Factor(i)
if len(f_list) % 2 == 0:
Luckynum.append(i)
# print(i,"的真因数:",f_list,"个数为",len(f_list),"个,■■是■■")
# else:
# print(i,"的真因数为:",f_list,"个数为",len(f_list),"个,♦♦不是♦♦") return Luckynum, sum(Luckynum)
main.py
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 19 19:32:33 2019 @author: Administrator
"""
import getf a = int(input("请输入最小值:"))
b = int(input("请输入最大值:"))
Luckynum, sum_Luckynum = getf.get_Luckynum(a,b)
print('区间 [{0},{1}] 的幸运数包含:{2}'.format(a, b, Luckynum))
print('它们的总和是:{0}'.format(sum_Luckynum))

另一个思路:被除数区间的定义
稍微思考一下
a*b = c a变大b就会变小 a变小b就会变大
假设a永远是最小 b永远是最大的哪个
那么 a 和 b 的它们最大值,肯定是 √c
该思路就是让 c 求商的时候,不用像常规思路一般一直除到本身(如65,要除以1..2..3..4......65 )资源消耗大,效率低下
而是一直除到 √c (如65,要除以1..2..3..4...一直到√65 就停止遍历)
当然 遍历到 √c 的时候 a = b 这个就在加个判断就好了,不允许重复
如下
import math def new_get_Factor(x):
if x == 0: return [0]
if x == 1: return [1]
f_list = []
for y in range(1,int(math.sqrt(x)) + 1):
#(1,根号x+1)确保能够遍历到根号x
rem = x % y
quo = x // y
if rem == 0:
f_list.append(y)
if y != quo:
f_list.append(quo)
continue
continue f_list.sort(reverse = False)
f_list.pop()
return x,f_list
Python 计算当真因子个数为偶数个时为幸运数,计算区间内幸运数之和的更多相关文章
- SPOJ DQUERY 求区间内不同数的个数 主席树
		
这题跟HDU3333差不多吧. 离线的做法很简单,不再说了 以前做过. 主席树的做法就比较暴力了.. 什么是主席树呢.. 其实是某种称号. 在该题中的体现是可持久化的线段树. 对于一个数 如果以前没出 ...
 - SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
		
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
 - 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
		
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
 - LightOj1028 - Trailing Zeroes (I)---求因子个数
		
题目链接:http://lightoj.com/volume_showproblem.php?problem=1028 题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它 ...
 - POJ 2992 求组合数的因子个数
		
求C(n,k)的因子个数 C(n,k) = (n*(n-1)*...*(n-k+1))/(1*2*...*k) = p1^k1 * p2^k2 * ... * pt^kt 这里只要计算出分子中素数因子 ...
 - Divisors_组合数因子个数
		
Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- ...
 - POJ 2992 Divisors (求因子个数)
		
题意:给n和k,求组合C(n,k)的因子个数. 这道题,若一开始先预处理出C[i][j]的大小,再按普通方法枚举2~sqrt(C[i][j])来求解对应的因子个数,会TLE.所以得用别的方法. 在说方 ...
 - Factors of Factorial AtCoder - 2286 (N的阶乘的因子个数)(数论)
		
Problem Statement You are given an integer N. Find the number of the positive divisors of N!, modulo ...
 - 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛-等式(求$N^2$的因子个数)
		
一.题目链接 https://www.nowcoder.com/acm/contest/90/F 二.题面 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言655 ...
 
随机推荐
- node.js 模块的分类
			
模块的简单分类可分为三类: 第一类分别为:核心模块:http.fs.path.... 第二类分别为:文件模块:var (util)=require('.util.js') 第三方类分别为:其他模块:v ...
 - vue_v-for_遍历数组_遍历对象
			
1. v-for 遍历数组 html <div id="test"> <ul> <li v-for="(p, index) in perso ...
 - codecademy课程笔记——JavaScript Promise
			
Promise是一种表示异步操作最终的结果的对象,一个Promise对象有三种状态 Pending: 初始状态 ,操作还未完成 Fullfilled:操作成功完成,且这个promise现在有一个r ...
 - mysql百万级全文索引及match快速查找
			
建立全文索引的表的存储引擎类型必须为MyISAM 问题是match against对中文模糊搜索支持不是太好 新建一个utf8 MyISAM类型的表并建立一个全文索引 : CREATE TABL ...
 - Nginx 自定义404、500、502 页面
			
利用nginx的反向代理来实现 服务器404 和500 等状态码的自定义页面 1.nginx配置文件 nginx.conf 配置开启代理错误拦截 和配置页面 下划线部分 http { ...... ...
 - Could not find artifact cn.e3mall:e3mall-parent:pom:0.0.1-SNAPSHOT
			
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[FATAL] Non-resolvable pare ...
 - char
			
1 char是多少位的 2 java用的是什么方式表示字符 3 Unicode是用多少位表示的 1的答案是16位的,2的答案是Unicode,3的答案是16位 值得注意的是,2的答案并不是utf-8 ...
 - Linux和Shell教程
			
文档资料参考: 参考:http://www.runoob.com/linux/linux-tutorial.html 软件下载参考: centos 下载地址:https://www.centos.or ...
 - Homework 7 INF 552
			
Homework 7 INF 552,1. Generative Models for Text(a) In this problem, we are trying to build a genera ...
 - js之history
			
浏览历史记录window.history,不会刷新页面内容,只会更改历史记录,用location.href 才会刷新 1. history.pushState() & history.repl ...