完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。
它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。
输入n,请输出n以内(含n)完全数的个数。
数据范围:
完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。
它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
输入一个数字n
输出不超过n的完全数的个数
1000
3
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n = sc.nextInt(); int cnt = 0; while((n--)>1) { int number = 0; for(int i=1; i<n; i++) { if(n%i==0) { number += i; } } if(n == number) { cnt++; } } if(cnt != 0) System.out.println(cnt); else System.out.println(-1); } } }
#include <bits/stdc++.h> using namespace std; int fun(int n) { int cnt=0; if(n>500000 || n<=0) return -1; else { for(int i=2;i<=n;i++) { int sum=0; for(int j=2;j<sqrt(i);j++) { if(i%j==0) { if(i/j==j) sum+=j; else { sum+=j; sum+=(i/j); } } } if(sum+1==i) cnt++; } } return cnt; } int main() { int n; while (cin >>n) { cout<<fun(n)<<endl; } system("pause"); return 0; }
#include<stdio.h> #include<math.h> int main() { int num; while(~scanf("%d", &num)){ if(num==1){printf("0\n");break;} int count = 0; for(int i=2; i<=num; i++){ int sum = 1,root = sqrt(i); // 求约数遍历到根值即可 for(int j=2; j<=root; j++){ if(i%j==0){ sum +=j; //与约数j相加 if(i!=root) //防止两个相同的约数重复计算 sum +=(i/j); //与约数j对应点另一个约数相加 } } if(sum == i)count++; } printf("%d\n", count); } }
while True: try: n = int(input()) l_n = [] # 用于储存不大于n的完全数 for i in range(5, n+1): #最小的完全数是6 = 1+2+3,所以从5开始,循环是为了判断每个不大于n的数是不是完全数 l_f = [] # 用于储存每次循环的 i 的因子 for j in range(2, int(i**0.5)+1): # 找i不含自身的因子,i**0.5 相当于 i开方 if i % j == 0: # 如果j 是 i 的因子 ,就把j 和 i//j储存起来 l_f.append(j) l_f.append(i // j) if i == 1 + sum(l_f): # 判断 i是否是是完全数,注意,加上1 是因为,储存因子的列表不包含1 l_n.append(i) # 是完全数,就存起来 l_f.clear() # 这一步很重要,清空储存因子的列表,因为每次判断i是不是完全数都会向该列表添加因子(除非i是质数) print(len(l_n)) # 题目要求输出 完全数的个数,即 l_n 列表的长度 except: break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); int count = 0; for(int i = 1; i <= n;i++){ if(isPerfect(i)){ count++; } } System.out.println(count); } } public static boolean isPerfect(int num){ int result = 0; for(int i = 1; i <= num/2;i++){ if(num % i==0 && num!=i){ result += i; } } if(result == num){ return true; } return false; } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n = sc.nextInt(); int count = 0; for(int i = 2; i <= n; i++) { if(isP(i)) count++; } System.out.println(count); } } public static boolean isP(int n) { int sum = 0; for(int i = 1; i * i <= n; i++) { if(n % i == 0) { int k = n / i; if(k == i) { sum += i; } else { sum += (i + k); } } } if(sum == 2 * n) { return true; } else { return false; } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { int a = scan.nextInt(); int count = 0; for (int i = 1; i <= a; i++) { int sum = 0; for (int j = 1; j <= Math.sqrt(i); j++) { if (i % j == 0) { sum += (j + i / j); } } if (sum == i * 2) { count += 1; } } System.out.println(count - 1); } } }
while True: try: n = int(input()) a = {} for i in range(1, n+1): a[i] = 1 for i in range(2, int(n**0.5)+1): a[i*i] = a[i*i] + i for j in range(i+1, n//i+1): a[i*j] = a[i*j] + i + j sum = 0 for i in range(2, n+1): if a[i] == i: sum += 1 print(sum) except: break
def is_perfectNum(num): L = [] for i in range(1,num): if num % i == 0: L.append(i) if sum(L) == num: return True return False while True: try: n = int(input().strip()) if 0 < n <= 500000: count_perfectNum = 0 for i in range(1, n+1): if is_perfectNum(i): count_perfectNum += 1 print(count_perfectNum) except: break
def checkprime(num): if num == 1: return False list1 = [] for i in range(2, num): if num % i == 0: list1.append(i) if not list1: return True else: return False while True: try: n = int(input()) res = [] for i in range(1, n+1): if (2**i-1)*2**(i-1) > n: break elif checkprime(i) and checkprime(2**i-1): res.append((2**i-1)*2**(i-1)) print(len(res)) except: break
def perfectnum(n): if n <= 5: return 0 cnt = 0 for i in range(6, n+1): add = 1 for j in range(2, int(i**0.5)+1): if i%j == 0: add = add + j + i//j if add == i: cnt += 1 return cnt while True: try: n = int(input()) print(perfectnum(n)) except: break
def is_perfect(num): yinzi = [] for i in range(1,num//2+1): if num%i==0: yinzi.append(i) if sum(yinzi)==num: return True else: return False while True: try: n = int(input()) if n<=0 or n>500000: print(-1) break else: count_ = 0 for num in range(2,n+1): if is_perfect(num): count_ += 1 print(count_) except: break
#900ms,还行吧,简单易懂 def pn(n): if n < 0&nbs***bsp;n > 500000: return -1 else: num = [] for i in range(1,int(n/2)+1): if n % i == 0: num.append(i) if sum(num) == n: return n else: return -1 while True: try: n = int(input()) count = 0 for i in range(1,n+1): if pn(i) == i: count += 1 print(count) except: break
#include <iostream> using namespace std; //把每一个数n 去一次对1-n/2个数取余,进行判断即可 int main() { int n; while(cin>>n) { int res = 0; for(int i = 1;i<=n;i++) { int tmp = 0; for(int j = 1;j<=i/2;j++) { if(i % j == 0) tmp+=j; } if(tmp == i) res++; } cout<<res<<endl; } return 0; }
#include<iostream> (720)#include<math.h> using namespace std; bool iscount(int n){ int sum=0; for(int i=1;i<n;i++){ if(n%i==0){ sum=sum+i; } } if(sum==n){ return true; }else{ return false; } } int main(){ int n; while(cin>>n){ int temp=0; for(int i=1;i<=n;i++){ if(iscount(i)){ temp++; } } cout<<temp<<endl; } }
from functools import reduce while True: try: n = int(input()) result = [] count = 0 if n > 1: for i in range(1,n+1): add_num = [] for j in range(1,i+1): if i % j == 0: add_num.append(j) add_num.remove(i) result.append(add_num) for i in range(2,n): if reduce(lambda x,y : x+y , result[i-1]) == i: count +=1 print(count) else: print(0) except: break