The problem is from codeforces: http://www.codeforces.com/problemset/problem/26/A
The input range is small which means even the worst brute-force methods can pass the test. The straightforward implementation is
The Python solution is presented, which solves the problem within 220 ms.
#!/usr/bin/env python
# https://helloacm.com
n = int(raw_input())
def chk(n):
i = 2
j = 0
k = 1
while n > 1:
u = n % i == 0
if u:
while n > 1:
n /= i # divisor extraction
if n % i > 0:
break
if i == 3:
k = 2 # skip even numbers
i += k
if u:
j += 1
if j > 2: # can't be almost prime
return False
return j == 2
s = 0
for i in xrange(6, n + 1):
if chk(i):
s += 1
print s
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Newton Iterative Sqrt Method
Next Post: E, base of the natural logarithm