📌  相关文章
📜  查找所有可以由L组成的拉曼努扬数字

📅  最后修改于: 2021-04-17 17:26:55             🧑  作者: Mango

给定正整数L ,任务是查找可以由任何四组(a,b,c,d)生成的所有Ramanujan数,其中0

例子:

天真的方法:最简单的方法是检查范围[1,L]中由满足等式a 3 + b 3 = c 3 + d 3的不同元素组成的四重组合(a,b,c,d)的所有组合。对于发现满足条件的元素,将Ramanujan数存储为3 + b 3 。最后,检查所有可能的组合后,打印所有存储的数字。

下面是上述方法的实现:

C++
// CPP program for the above approach
#include
using namespace std;
 
// Function to find Ramanujan numbers
// made up of cubes of numbers up to L
map> ramanujan_On4(int limit)
{
    map> dictionary;
 
    // Generate all quadruples a, b, c, d
    // of integers from the range [1, L]
    for(int a = 0; a < limit; a++)
    {
        for(int b = 0; b < limit; b++)
        {
            for(int c = 0; c < limit; c++)
            {
               for(int d = 0; d < limit; d++)
               {
 
                    // Condition // 2:
                    // a, b, c, d are not equal
                    if ((a != b) and (a != c) and (a != d)
                        and (b != c) and (b != d)
                            and (c != d)){
 
                        int x = pow(a, 3) + pow(b, 3);
                        int y = pow(c, 3) + pow(d, 3);
                        if ((x) == (y))
                        {
                            int number = pow(a, 3) + pow(b, 3);
                            dictionary[number] = {a, b, c, d};
                        }
                    }
            }
        }
    }
}
 
    // Return all the possible number
    return dictionary;
}
 
// Driver Code
int main()
{
    
// Given range L
int L = 30;
map> ra1_dict = ramanujan_On4(L);
 
// Print all the generated numbers
for(auto x:ra1_dict)
{
    cout << x.first << ": (";
   
   // sort(x.second.begin(),x.second.end());
    for(int i = x.second.size() - 1; i >= 0; i--)
    {
        if(i == 0)
          cout << x.second[i] << ")";
        else
         cout << x.second[i] << ", ";   
    }
    cout << endl;
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
static Map> ra1_dict; 
 
// Function to find Ramanujan numbers
// made up of cubes of numbers up to L
static void ramanujan_On4(int limit)
{
     
    // Generate all quadruples a, b, c, d
    // of integers from the range [1, L]
    for(int a = 0; a < limit; a++)
    {
        for(int b = 0; b < limit; b++)
        {
            for(int c = 0; c < limit; c++)
            {
               for(int d = 0; d < limit; d++)
               {
 
                    // Condition // 2:
                    // a, b, c, d are not equal
                    if ((a != b) && (a != c) && (a != d) &&
                        (b != c) && (b != d) && (c != d))
                    {
                        int x = (int)Math.pow(a, 3) +
                                (int) Math.pow(b, 3);
                        int y = (int)Math.pow(c, 3) +
                                (int) Math.pow(d, 3);
                        if ((x) == (y))
                        {
                            int number = (int)Math.pow(a, 3) +
                                         (int) Math.pow(b, 3);
                            ra1_dict.put(number, new ArrayList<>(
                                Arrays.asList(a, b, c, d)));
                        }
                    }
                }
            }
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given range L
    int L = 30;
     
    ra1_dict = new HashMap<>();
     
    ramanujan_On4(L);
     
    // Print all the generated numbers
    for(Map.Entry> x: ra1_dict.entrySet())
    {
        System.out.print(x.getKey() + ": (");
         
        // sort(x.second.begin(),x.second.end());
        for(int i = x.getValue().size() - 1; i >= 0; i--)
        {
            if (i == 0)
                System.out.print(x.getValue().get(i) + ")");
            else
                System.out.print(x.getValue().get(i) + ", ");   
        }
        System.out.println();
    }
}
}
 
// This code is contributed by offbeat


Python3
# Python program for the above approach
import time
 
# Function to find Ramanujan numbers
# made up of cubes of numbers up to L
def ramanujan_On4(limit):
    dictionary = dict()
 
    # Generate all quadruples a, b, c, d
    # of integers from the range [1, L]
    for a in range(0, limit):
        for b in range(0, limit):
            for c in range(0, limit):
                for d in range(0, limit):
 
                    # Condition # 2:
                    # a, b, c, d are not equal
                    if ((a != b) and (a != c) and (a != d)
                        and (b != c) and (b != d)
                            and (c != d)):
 
                        x = a ** 3 + b ** 3
                        y = c ** 3 + d ** 3
                        if (x) == (y):
                            number = a ** 3 + b ** 3
                            dictionary[number] = a, b, c, d
 
    # Return all the possible number
    return dictionary
 
 
# Driver Code
 
# Given range L
L = 30
ra1_dict = ramanujan_On4(L)
 
# Print all the generated numbers
for i in sorted(ra1_dict):
    print(f'{i}: {ra1_dict[i]}', end ='\n')


Python3
# Python program for the above approach
from array import *
import time
 
# Function to find Ramanujan numbers
# made up of cubes of numbers up to L
def ramanujan_On2(limit):
    cubes = array('i', [])
 
    # Stores the sum of pairs of cubes
    dict_sum_pairs = dict()
 
    # Stores the Ramanujan Numbers
    dict_ramnujan_nums = dict()
    sum_pairs = 0
 
    # Stores the cubes from 1 to L
    for i in range(0, limit):
        cubes.append(i ** 3)
      
    # Generate all pairs (a, b)
    # from the range [0, L]
    for a in range(0, limit):
        for b in range(a + 1, limit):
            a3, b3 = cubes[a], cubes[b]
 
            # Find the sum of pairs
            sum_pairs = a3 + b3
 
            # Append to dictionary
            if sum_pairs in dict_sum_pairs:
 
                # If the current sum is in
                # the dictionary, then store
                # the current number
                c, d = dict_sum_pairs.get(sum_pairs)
                dict_ramnujan_nums[sum_pairs] = a, b, c, d
 
            # Otherwise append the current
            # sum pairs to the sum pairs
            # dictionary
            else:
                dict_sum_pairs[sum_pairs] = a, b
 
        # Return the possible Ramanujan
    # Numbers
    return dict_ramnujan_nums
 
 
# Driver Code
 
# Given range L
L = 30
r_dict = ramanujan_On2(L)
 
# Print all the numbers
for d in sorted(r_dict):
    print(f'{d}: {r_dict[d]}', end ='\n')


输出:
1729: (9, 10, 1, 12)
4104: (9, 15, 2, 16)
13832: (18, 20, 2, 24)
20683: (19, 24, 10, 27)

时间复杂度: O(L 4 )
辅助空间: O(1)

高效的方法:上述方法也可以通过使用哈希进行优化。请按照以下步骤解决问题:

  • 初始化一个数组,例如ans [] ,以存储满足给定条件的所有可能的拉曼努安数。
  • 将范围为[1,L]的所有数字的多维数据集预先计算并存储在辅助数组arr []中
  • 初始化一个HashMap,例如M ,它存储从数组arr []生成的一对多数据集的所有可能组合的总和。
  • 现在,生成数组arr []的所有可能的对(i,j),如果数组中不存在对的总和,则在Map中标记当前对的总和的出现。否则,将当前和添加到数组ans []中,因为它是Ramanujan Number之一。
  • 完成上述步骤后,打印存储在数组ans []中的数字

下面是上述方法的实现:

Python3

# Python program for the above approach
from array import *
import time
 
# Function to find Ramanujan numbers
# made up of cubes of numbers up to L
def ramanujan_On2(limit):
    cubes = array('i', [])
 
    # Stores the sum of pairs of cubes
    dict_sum_pairs = dict()
 
    # Stores the Ramanujan Numbers
    dict_ramnujan_nums = dict()
    sum_pairs = 0
 
    # Stores the cubes from 1 to L
    for i in range(0, limit):
        cubes.append(i ** 3)
      
    # Generate all pairs (a, b)
    # from the range [0, L]
    for a in range(0, limit):
        for b in range(a + 1, limit):
            a3, b3 = cubes[a], cubes[b]
 
            # Find the sum of pairs
            sum_pairs = a3 + b3
 
            # Append to dictionary
            if sum_pairs in dict_sum_pairs:
 
                # If the current sum is in
                # the dictionary, then store
                # the current number
                c, d = dict_sum_pairs.get(sum_pairs)
                dict_ramnujan_nums[sum_pairs] = a, b, c, d
 
            # Otherwise append the current
            # sum pairs to the sum pairs
            # dictionary
            else:
                dict_sum_pairs[sum_pairs] = a, b
 
        # Return the possible Ramanujan
    # Numbers
    return dict_ramnujan_nums
 
 
# Driver Code
 
# Given range L
L = 30
r_dict = ramanujan_On2(L)
 
# Print all the numbers
for d in sorted(r_dict):
    print(f'{d}: {r_dict[d]}', end ='\n')
输出:
1729: (9, 10, 1, 12)
4104: (9, 15, 2, 16)
13832: (18, 20, 2, 24)
20683: (19, 24, 10, 27)

时间复杂度: O(L 2 )
辅助空间: O(L 2 )