R program to find perfect numbers upto n numbers and in the given range

25 Feb, 2021
Given a range the task is to write a R program to print all perfect numbers in that range.
A positive integer that is equal to the sum of its proper divisors is known as a perfect number. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. Please note that all the perfect square numbers end with 0, 1, 4, 5, 6 or 9 but all the numbers with end with 0, 1, 4, 5, 6 or 9 thus perfect numbers are not perfect square numbers. Example, 11, 21, 51, 79, 76 etc. are the numbers which are not perfect square numbers.
Algorithm:
Fix interval
Find factors of the number
Find sum
Check equivalence
Choose accordingly
Increment number
Keeping going in this fashion until end of the interval is reached.
Example :
Find the perfect and non-perfect numbers upto 10 numbers starting from 1 ?
Output:
[1] “1 is not a perfect number”
[1] “2 is not a perfect number”
[1] “3 is not a perfect number”
[1] “4 is not a perfect number”
[1] “5 is not a perfect number”
[1] “6 is a perfect number”
[1] “7 is not a perfect number”
[1] “8 is not a perfect number”
[1] “9 is not a perfect number”
[1] “10 is not a perfect number” 
Example 1: 
R
for (k in 50:100) {    n = k    i = 1    s = 0      while (i < n) {      if (n %% i == 0) {        s = s + i      }      i = i + 1    }    if (s == n) {      print(paste(n,"is a perfect number"))    } else{      print(paste(n,"is not a perfect number"))    }k=k+1}
Output:
[1] “1 is not a perfect number”
[1] .
[1] .
[1] “6 is a perfect number”
[1] “7 is not a perfect number”
[1] .
[1] .
[1] “27 is not a perfect number”
[1] “28 is a perfect number”
[1] .
[1] .
[1] “495 is not a perfect number”
[1] “496 is a perfect number”
[1] .
[1] .
[1] “500 is not a perfect number”
Example 2:
R
perfect_numbers = c()non_perfect_numbers=c()for (k in 1:100) {    n <- k    i = 1    s = 0      while (i < n) {      if (n %% i == 0) {        s = s + i      }      i = i + 1    }      if (s == n) {      perfect_numbers = c(perfect_numbers, n)    } else{      non_perfect_numbers = c(non_perfect_numbers, n)    }}print(perfect_numbers)print(non_perfect_numbers)k=k+1
Output:
(perfect_numbers)
[1]  6 28
(non_perfect_numbers)
 [1]   1   2   3   4   5   7   8   9  10  11  12  13  14  15  16  17  18  19  20
[20]  21  22  23  24  25  26  27  29  30  31  32  33  34  35  36  37  38  39  40
[39]  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59
[58]  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78
[77]  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97
[96]  98  99 100
R-Mathematics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
How to filter R DataFrame by values in a column?
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming