Ruth-Aaron numbers
Last Updated :
16 Jul, 2021
A number N is said to be Ruth-Aaron numbers if sum of prime divisors of N is equal to the sum of prime divisors of N+1.
The first few Ruth-Aaron numbers are:
5, 24, 49, 77, 104, 153, 369, 492, 714……..
Check if N is a Ruth-Aaron number
Given a number N, the task is to find if this number is Ruth-Aaron number or not.
Examples:
Input: N = 714
Output: YES
Input: N = 25
Output: No
Approach: The idea is to find the sum of all proper divisors of N and N + 1 and check if the sums of proper divisors of N and N+1 are equal or not. If the sum of proper divisors of N and N+1 are equal then the number is Ruth-Aaron number.
For Example:
For N = 714
Sum of Proper Divisors of N (714) = 2 + 3 + 7 + 17 = 29
Sum of Proper Divisors of N+1 (715) = 5 + 11 + 13 = 29
Therefore, N is a Ruth-Aaron number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Sum( int N)
{
int SumOfPrimeDivisors[N + 1] = { 0 };
for ( int i = 2; i <= N; ++i) {
if (!SumOfPrimeDivisors[i]) {
for ( int j = i; j <= N; j += i) {
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
bool RuthAaronNumber( int n)
{
if (Sum(n) == Sum(n + 1))
return true ;
else
return false ;
}
int main()
{
int N = 714;
if (RuthAaronNumber(N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static int Sum( int N)
{
int SumOfPrimeDivisors[] = new int [N + 1 ];
for ( int i = 2 ; i <= N; ++i)
{
if (SumOfPrimeDivisors[i] == 1 )
{
for ( int j = i; j <= N; j += i)
{
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
static boolean RuthAaronNumber( int n)
{
if (Sum(n) == Sum(n + 1 ))
return true ;
else
return false ;
}
public static void main (String[] args)
{
int N = 714 ;
if (RuthAaronNumber(N))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def Sum (N):
SumOfPrimeDivisors = [ 0 ] * (N + 1 )
for i in range ( 2 , N + 1 ):
if (SumOfPrimeDivisors[i] = = 0 ):
for j in range (i, N + 1 , i):
SumOfPrimeDivisors[j] + = i
return SumOfPrimeDivisors[N]
def RuthAaronNumber(n):
if ( Sum (n) = = Sum (n + 1 )):
return True
else :
return False
N = 714
if (RuthAaronNumber(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int Sum( int N)
{
int []SumOfPrimeDivisors = new int [N + 1];
for ( int i = 2; i <= N; ++i)
{
if (SumOfPrimeDivisors[i] == 1)
{
for ( int j = i; j <= N; j += i)
{
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
static bool RuthAaronNumber( int n)
{
if (Sum(n) == Sum(n + 1))
return true ;
else
return false ;
}
public static void Main()
{
int N = 714;
if (RuthAaronNumber(N))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function Sum( N) {
let SumOfPrimeDivisors = Array(N + 1).fill(0);
for ( let i = 2; i <= N; ++i) {
if (SumOfPrimeDivisors[i] == 1) {
for (let j = i; j <= N; j += i) {
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
function RuthAaronNumber( n) {
if (Sum(n) == Sum(n + 1))
return true ;
else
return false ;
}
let N = 714;
if (RuthAaronNumber(N)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Reference: https://oeis.org/A006145
Share your thoughts in the comments
Please Login to comment...