Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number accuracy problem?
#6
(Dec-23-2021, 10:28 PM)Gribouillis Wrote: There are many issues in your code, before I talk about them, here is a code that I wrote to solve this problem, inspired by your code. It finds the solution 55374 in about 8 seconds in my terminal, but I made no attempt to optimize it.
import numpy as np
import itertools as itt

def ipentagonal():
    s = 0
    for c in itt.count(1):
        s += c
        if s % 3 == 0:
            yield s // 3

N = 1000000
#N = 100
gstore = np.fromiter(itt.takewhile(lambda x: x <= 2*N, ipentagonal()), int)
sign = np.fromiter(
    (1 if (i%4 <= 1) else -1 for i in range(gstore.size)), int)
pstore = np.zeros(N, int)
pstore[0] = 1
print(gstore)
print(sign)

for n in range(1, N):
    idx = itt.takewhile(lambda x: x>=0, (n-g for g in gstore))
    pstore[n] = sum(pstore[i] * sign[j] for j, i in enumerate(idx)) % 1000000
    if pstore[n] == 0:
        print(n)
        break
#######

The main problems that I see in your code are
  • You are using numpy arrays containing floating point values, that is to say 64 bits numbers. This will lose precision if you store large integers in them. This is an integer valued problem, so you need arrays that contain integer values, this is what I use in my code. Note that Python integers have no size limitation other than the machine's memory, but this is not the case of Numpy integers which have a fixed size, such as 32 or 64 bits.
  • The partition number increases very rapidly, according to Wikipedia, p(10000) has already 106 ordinary decimal digits. It means that you must not compute p(n), but for this problem you can simply compute p(n) % 1000000, which is what my code does.

#####

Note: By removing the final 'break' statement, the above code found the next solution after 55374, namely 488324.

Thanks for your reaction and explanation. I found that the problem is indeed in the Numpy arrays containing floating point values. I have removed all Numpy arrays and replaced them with "normal" arrays. Now everything works fine!
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,222 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Problem : Count the number of Duplicates NeedHelpPython 3 4,537 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Calculating accuracy on RNN-LSTM model hobbyist 0 6,275 May-14-2021, 08:55 AM
Last Post: hobbyist
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,242 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  problem with complex number jodrickcolina 1 2,386 Apr-13-2019, 06:59 PM
Last Post: Yoriz
  Measure accuracy of Object Detection Shameendra 2 2,773 Nov-19-2018, 01:04 PM
Last Post: Shameendra
  can't figure out problem with number guess Galoxys 4 3,445 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Accuracy of sqrt vndywarhol 1 2,597 Aug-29-2018, 10:14 AM
Last Post: Gribouillis
  why not accuracy working rajeev1729 1 2,670 Sep-12-2017, 02:15 PM
Last Post: ichabod801
  Prime number Script Problem Codezters 4 4,152 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