You are not logged in. Please login at www.codechef.com to post your questions!

×

NUMFACT Wrong Answer

Hello,
I am trying to solve NUMFACT, but it is giving me Runtime Error NZEC
My code seems to run perfectly fine for all cases I can think of but it is giving me an NZEC in cases after the first case in subtask 1.
Submission link

import math
T = int(raw_input())

for i in range(T):
    raw_input()
    p = [int(_) for _ in raw_input().split(" ")]
    p1 = 1
    for p_ in p:
            p1 *= p_
    facs = 2
    sqr = int(math.sqrt(p1))
    for j in range(2, sqr+1):
        if p1%j==0: facs+=2
    if sqr*sqr==p1: facs-=1
    print facs

Please tell me what cases I'm missing.

asked 20 Feb '14, 12:37

svineet's gravatar image

3★svineet
362410
accept rate: 0%


@svineet : Try this test case
1
5
1000000 10000000 1000000 10000000 1000000
Your program gives
Traceback (most recent call last):
File "prog.py", line 12, in <module>
OverflowError: range() result has too many items


Line 12 of your program is
for j in range(2, sqr+1):

Each number can be 10^6 and there can be 10 numbers so product can be 10^60 .
Square root of 10^60 is 10^30
You can't iterate over these many numbers .
You have to improve your logic
I have done this problem , so i can tell you the logic , but don't want to spoil your fun solving it .
So I will give you the logic if you request .
Best of luck , Happy Coding .
PS : Sorry about earlier wrong comment , I haven't used python that's why i was not aware of this.

link

answered 20 Feb '14, 14:18

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

no need for the logic, after all the fun is in figuring it out ourself :)

(20 Feb '14, 20:31) svineet3★

@svineet : You are having integer overflow .
Constraints :
1 ≤ N ≤ 10
2 ≤ Ai ≤ 1000000
So product of all numbers can go out of range for int .
Hence it may become negative also .
So when you take square root you get run time error .

link

answered 20 Feb '14, 12:40

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

there is no limit on ints in python.

(20 Feb '14, 13:50) svineet3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×553
×528
×86
×30

question asked: 20 Feb '14, 12:37

question was seen: 909 times

last updated: 20 Feb '14, 20:31