Project Euler #62: Cubic permutations

Sort by

recency

|

7 Discussions

|

  • + 1 comment

    I don't understand why my code doesn't work?!

    from collections import defaultdict
    d=defaultdict(lambda:[])
    
    n,k=map(int,input().split(' '))
    
    l=[]
    for i in range(1,n):
        temp=i*i*i
        s="".join(sorted(str(temp)))
        d[s].append(i)
        if len(d[s])==k:
            #d[s].sort()
            l.append(pow(min(d[s]),3))
        
    #print(d)
    new=sorted(l)
    for v in new:
        print(v)
    
    • + 1 comment

      The problem says, 'exactly' k, but in your code you add every number that reachs k.

      from collections import defaultdict
      d=defaultdict(lambda:[])
      
      n,k=map(int,input().split(' '))
      
      l=[]
      for i in range(1,n):
          temp=i*i*i
          s="".join(sorted(str(temp)))
          d[s].append(i)
      
      for item in d.items():
          if len(item[1]) == k:
              l.append(pow(min(item[1]), 3))
      
      new=sorted(l)
      for v in new:
          print(v)
      
      • + 0 comments

        Omg it worked! I realized the mistake. Thank you so much!!!

  • + 0 comments

    Hint: Try to use hash/dictionary to store data, then you can save a lot of running time!

    For example: {'01234566': [41063625, 56623104, 66430125]} within:

    41063625 = 345^3; 56623104 = 384^3; 66430125 = 405^3

    '01234566' is a sorted string that contains all digits

  • + 0 comments

    If you're failing tests 0, 3, 5 but passing all others it's probably because you included entries larger than n in your permutations. For example, {1, 2, 3} is k=3, but for n = 2, k for the list is only 1 as both 2 and 3 are larger or equals to n.

  • + 1 comment

    Hi, can anyone please give more testcases with max valus of N and K? I am failing test-cases from #5 to #9.

    • + 0 comments

      This should work:

      1000000 49 ==> 101740954697838253
      
  • + 2 comments

    please suggest improvements.....can only pass 0 and 1st cases

    i=0;
    cubes=[]
    n,k=map(int,input().split(' ',1))
    while(1):
        cube=sorted(list(str(i**3)))
        cubes.append(cube)
        if(cubes.count(cube)==k):
            print(pow(cubes.index(cube),3))
            break
        i=i+1
    
    • + 1 comment

      my logic is almost same as yours and i am able to pass first 3 test cases only. can anyone give a hint.

      • + 0 comments

        There may be more solutions.

    • + 0 comments

      Don't use break since there can be many sets having permutation k