perfect-numbers

Perfect Number In C

吃可爱长大的小学妹 提交于 2021-02-18 12:48:05
问题 I need to write a C program to find the Perfect Number.. main() { int n=1000,sum = 0; for(int num = 1; num <= n; num++) { sum = 0; for(int i = 1; i < num; i++) { if(!(num%i)) { sum+=i; } } if(sum == num) printf("\n%d",num); } } if(!(num%i)) - This is d line I do not understand. If there is any other simple method do please suggest me 回答1: if(!(num%i)) simply means if( (num%i) == 0 ) 回答2: If you are looking for a more efficient way to find perfect numbers, you might want to read the Wikipedia

Java program to find perfect numbers below 10,000 [closed]

▼魔方 西西 提交于 2020-06-29 05:32:32
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Improve this question I am currently working on java code that will allow me to print out all perfect numbers below 10,000. My issue is that I can not figure out why my code is not printing 6, but is printing all the other perfect numbers. My code is below, please send help if you

Write a function in R to find perfect numbers

喜你入骨 提交于 2020-01-07 02:34:08
问题 I am a beginner in R and am attempting the following question: Create a function in R which takes as its input a natural number N and returns as an output the list of all perfect numbers between 1 and N. There are 3 steps here: 1. Check the list of factors 2. Check whether it is a perfect number 3.check from 1 to 10000 factorlist<-function(n){ if(n<2){return("Invalid Input")} if(n%%1!=0){return("Invalid Input")} vec<-0 for(i in 1:(n-1)){ if(n%%i==0){ vec[length(vec)]<-i vec<-c(vec,0) } } vec<

Testing if an inputted Int is a perfect number

白昼怎懂夜的黑 提交于 2019-12-31 07:27:06
问题 I've been looking into perfect numbers, and I found this interesting bit of code on rosettacode: perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0] Now, I understand what a perfect number is, and I know which numbers are considered perfect, however I'm struggling to work out which parts of this code do what. I understand that it's working out the factors of the inputted number, and combining them together to see if it matches the input itself, but I'm not sure how it's doing this. If

Algorithm to check if a number if a perfect number

℡╲_俬逩灬. 提交于 2019-12-19 02:55:15
问题 I am looking for an algorithm to find if a given number is a perfect number. The most simple that comes to my mind is : Find all the factors of the number Get the prime factors [except the number itself, if it is prime] and add them up to check if it is a perfect number. Is there a better way to do this ?. On searching, some Euclids work came up, but didnt find any good algorithm. Also this golfscript wasnt helpful: https://stackoverflow.com/questions/3472534/checking-whether-a-number-is

Perfect numbers. Something wrong

无人久伴 提交于 2019-12-13 22:24:58
问题 I am trying to accomplish a task. To print 4 perfect numbers which are between 1 and 10000. In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. Here's my code: public class PerfectNumbers { public static void main(String[] args) { // Perfect numbers! for (int number = 1; number < 10000; number++) { int sum = 0; int i = 1; while (i < number) { if (number % i ==

Program crashes when `if (variable % 2 == 0)`

一笑奈何 提交于 2019-12-13 01:35:02
问题 I'm writing a program that finds perfect numbers . Having read about these perfect numbers I came across a list of them: List of perfect numbers. At the moment the output is: 28 // perfect 496 // perfect 8128 // perfect 130816 // not perfect 2096128 // not perfect 33550336 // perfect I decided to create array and put it with numbers, which divide the number wholly (without the rest). So I will be able to verify if it is a perfect number or not by adding all elements of the array. But app

Finding perfect numbers (optimization)

≯℡__Kan透↙ 提交于 2019-12-12 10:54:25
问题 I coded up a program in C# to find perfect numbers within a certain range as part of a programming challenge . However, I realized it is very slow when calculating perfect numbers upwards of 10000. Are there any methods of optimization that exist for finding perfect numbers? My code is as follows: using System; using System.Collections.Generic; using System.Linq; namespace ConsoleTest { class Program { public static List<int> FindDivisors(int inputNo) { List<int> Divisors = new List<int>();

deciding if a number is perfect or prime

旧街凉风 提交于 2019-12-11 02:37:49
问题 the problem is : "Write a function to find out if a number is a prime or perfect number." so far i have worked on the perfect part first and this is what i have: #include <iostream> using namespace std; bool perfectNumber(int); int main() { int number; cout<<"Please enter number:\n"; cin>>number; bool perfectNumber(number); return 0; } bool perfectNumber(int number) { int i; int sum=0; for(i=1;i<=number/2;i++) { if(number%i==0) { sum+=i; } } if (sum==number) return i; else return 0; } HOWEVER

SCHEME recursion perfect number (beginner, hopefully easy fix)

北城余情 提交于 2019-12-11 01:49:04
问题 having an issue with my perfect number function. The objective of the code is to determine if the number is a perfect number, meaning it is equal to the sum of its divisors. Ex:6. Im having trouble with my code. Here's my function: (define (is-perfect x) (define (divides a b) (= (modulo b a) 0)) (define (sum-proper-divisors y) (if (= y 1) 1 (if (divides y x) (+ y (sum-proper-divisors (- y 1))) (if (= x 1) #f (= (sum-proper-divisors (- x 1) x))))))) 回答1: You almost got it! there are a couple