 Open in App
Not now

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

• Difficulty Level : Hard
• Last Updated : 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 is not a perfect number”

 “2 is not a perfect number”

 “3 is not a perfect number”

 “4 is not a perfect number”

 “5 is not a perfect number”

 “6 is a perfect number”

 “7 is not a perfect number”

 “8 is not a perfect number”

 “9 is not a perfect number”

 “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 is not a perfect number”

 .

 .

 “6 is a perfect number”

 “7 is not a perfect number”

 .

 .

 “27 is not a perfect number”

 “28 is a perfect number”

 .

 .

 “495 is not a perfect number”

 “496 is a perfect number”

 .

 .

 “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)

  6 28

(non_perfect_numbers)

   1   2   3   4   5   7   8   9  10  11  12  13  14  15  16  17  18  19  20

  21  22  23  24  25  26  27  29  30  31  32  33  34  35  36  37  38  39  40

  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59

  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78

  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97

  98  99 100

My Personal Notes arrow_drop_up
Related Articles