Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number accuracy problem?
#1
Hi all,

I am trying to solve problem 78 from Project Euler (https://projecteuler.net/problem=78). The problem is related to integer partition. The question here is to find the first number, n, for which p(n) is divisible by 1e6. I have tested my algorithm for a small number of coins (<100) and it seems to work fine (it produces the correct results). My idea was to use the modulo operator to determine the number of coins for which the partition function = 0. However, I get the wrong results over and over again... I compared my results with other algorithms that produce the correct number, and it seems that it is related to the accuracy of the numbers that are determined by my algorithm. The answer should be 55374, but my algorithm finds the first n to be 4096. Any idea what is wrong here? Note that I have no experience with Python, so I haven't used any functions or whatever yet.

import numpy as np

# number of coins
N = 100000

# pre-allocate some arrays
# Pstore contains the partition values
# gstore contains the generalized pentagonal numbers
# signs contains alternating signs
Pstore = np.zeros((N,1))
gstore = np.zeros((N,1))
signs  = np.zeros((5*N,1))

# create array with alternating signs
for n in range(0,N):
    
    signs[4*n+1] = 1
    signs[4*n+2] = 1
    signs[4*n] = -1
    signs[4*n+3] = -1
signs = np.delete(signs, 0)

# start loop
for n in range (0,N):
    
    # partition function = 1 for n = 0 and n = 1
    if n == 0:
        
        Pstore[n] = 1
        
    elif n == 1:
        Pstore[n] = 1
        gstore[n] = 1
        
    # calculate generalized pentagonal number
    elif n > 1:
        
        if n % 2 == 0:
            
            gstore[n] = (3*(-int(n/2))**2-(-int(n/2)))/2
            
        else:
            
            gstore[n] = (3*(int(n/2)+1)**2-(int(n/2)+1))/2
            

        a = -1
        counter = 0
        # calculate partition number and store
        while a < 0:
            
            
            if n > gstore[counter+1]:
                
                Pstore[n] += signs[counter] * Pstore[int(n-gstore[counter+1])]
                
                counter += 1
                
            elif n == gstore[counter+1]:
                
                Pstore[n] += signs[counter] * Pstore[int(n-gstore[counter+1])]
                a = 0
                
            elif n < gstore[counter+1]:
            
                a = 0
    
    # print value if number is found
    if Pstore[n] % 1000000 == 0:
        print(Pstore[n])
Reply


Messages In This Thread
number accuracy problem? - by roym - Dec-23-2021, 01:49 PM
RE: number accuracy problem? - by deanhystad - Dec-23-2021, 05:17 PM
RE: number accuracy problem? - by roym - Dec-24-2021, 07:55 AM
RE: number accuracy problem? - by Gribouillis - Dec-23-2021, 10:28 PM
RE: number accuracy problem? - by roym - Dec-24-2021, 07:57 AM
RE: number accuracy problem? - by Gribouillis - Dec-24-2021, 06:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,173 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Problem : Count the number of Duplicates NeedHelpPython 3 4,486 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Calculating accuracy on RNN-LSTM model hobbyist 0 6,235 May-14-2021, 08:55 AM
Last Post: hobbyist
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,214 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  problem with complex number jodrickcolina 1 2,360 Apr-13-2019, 06:59 PM
Last Post: Yoriz
  Measure accuracy of Object Detection Shameendra 2 2,741 Nov-19-2018, 01:04 PM
Last Post: Shameendra
  can't figure out problem with number guess Galoxys 4 3,418 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Accuracy of sqrt vndywarhol 1 2,581 Aug-29-2018, 10:14 AM
Last Post: Gribouillis
  why not accuracy working rajeev1729 1 2,652 Sep-12-2017, 02:15 PM
Last Post: ichabod801
  Prime number Script Problem Codezters 4 4,113 Jul-21-2017, 03:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020