Problem:


Fermat's Last Theorem

In the 17th century, Fermat wrote that he proved for any integer $n \geq 3$,
there exist no positive integers $x$, $y$, $z$ such that
$x^n + y^n = z^n$.
However he never disclosed the proof.  Later, this claim was named
Fermat's Last Theorem or Fermat's Conjecture.

If Fermat's Last Theorem holds in case of $n$, then it also holds
in case of any multiple of $n$.
Thus it suffices to prove cases where $n$ is a prime number
and the special case $n$ = 4.

A proof for the case $n$ = 4 was found in Fermat's own memorandum.
The case $n$ = 3 was proved by Euler in the 18th century.
After that, many mathematicians attacked Fermat's Last Theorem.
Some of them proved some part of the theorem, which was a
partial success.
Many others obtained nothing.
It was a long history.
Finally, Wiles proved Fermat's Last Theorem in 1994.

Fermat's Last Theorem implies that for any integers $n \geq 3$
and $z > 1$, it always holds that

$z^n > $ max { $x^n + y^n | x > 0, y > 0, x^n + y^n \leq z^n$ }.

Your mission is to write a program that verifies this in the case
$n$ = 3 for a given $z$.  Your program should read in
integer numbers greater than 1, and, corresponding to each input
$z$, it should output the following:

$z^3 - $ max { $x^3 + y^3 | x > 0, y > 0, x^3 + y^3 \leq z^3$ }.

Input

The input is a sequence of lines each containing one positive integer
number followed by a line containing a zero.  You may assume that all of
the input integers are greater than 1 and less than 1111.

Output

The output should consist of lines each containing a single
integer number.  Each output integer should be

$z^3 - $ max { $x^3 + y^3 | x > 0, y > 0, x^3 + y^3 \leq z^3$ }.

for the corresponding input integer z.
No other characters should appear in any output line.

Sample Input

6
4
2
0

Output for the Sample Input

27
10
6



Sample input:
6
4
2
0


Sample output:
27
10
6

Submission status: Time Limit Exceeded

Solution code:
while(1):
    z=int(raw_input())
    if z==0:
        break
    else:
        k=0
        for x in range(1,z+1):
            for y in range(1,z+1):
                if x**3+y**3>z**3:
                    break
                k=max(k,x**3+y**3)
        print z**3-k

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement as C
while True:
    z = int(input())
    if z == 0:
        break
    zzz = pow(z, 3)
    candidates = []
    combinations = list(C(list(range(1, z+1)), 2))
    for xy in combinations:
        x, y = xy[0], xy[1]
        tmp = pow(x, 3)+pow(y, 3)
        if zzz >= tmp:
            candidates.append(tmp)
    print(zzz-max(candidates))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement as C
while True:
    z = int(input())
    if z == 0:
        break
    zzz = pow(z, 3)
    candidates = []
    combinations = list(C(list(range(1, z+1)), 2))
    if len(combinations) >= 100:
        conbinations = combinations[100:]
    for xy in combinations:
        x, y = xy[0], xy[1]
        tmp = pow(x, 3)+pow(y, 3)
        if zzz >= tmp:
            candidates.append(tmp)
    print(zzz-max(candidates))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement as C
while True:
    z = int(input())
    if z == 0:
        break
    zzz = pow(z, 3)
    candidates = []
    combinations = list(C(list(range(1, z+1)), 2))
    if z >= 100:
        mid = len(combinations) // 2
        combinations = combinations[mid:]
    for xy in combinations:
        x, y = xy[0], xy[1]
        tmp = pow(x, 3)+pow(y, 3)
        if zzz >= tmp:
            candidates.append(tmp)
    print(zzz-max(candidates))

Submission status: Time Limit Exceeded

Solution code:
while True:
    z=input()
    if z==0:break
    z3=pow(z,3)
    maxInt=0
    for x in range(1,z):
        x3=pow(x,3)
        for y in range(1,z):
            y3=pow(y,3)
            if x3+y3<=z3:
                maxInt=max(maxInt,x3+y3)
    print z3-maxInt

Submission status: Time Limit Exceeded

Solution code:
while 1:
	n=input()
	n3=n**3
	if n==0:break
	mn=n3
	i=j=0
	while 1:
		i+=1
		j=i
		if n3-i**3-j**3<0:break
		while 1:
			j+=1
			m=n3-i**3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Submission status: Time Limit Exceeded

Solution code:
while 1:
	n=input()
	n3=n**3
	if n==0:break
	mn=n3
	i=j=0
	while 1:
		i+=1
		j=i
		i3=i**3
		if n3-i3-j**3<0:break
		while 1:
			j+=1
			m=n3-i3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Submission status: Accepted

Solution code:
# AOJ 1109: Fermat's Last Theorem
# Python3 2018.7.14 bal4u

from bisect import bisect_left

tbl = [i**3 for i in range(1111)]
while True:
	z = int(input())
	if z == 0: break
	ma = 0
	for x in range(z-1, 0, -1):
		y = bisect_left(tbl, tbl[z]-tbl[x])-1
		if tbl[y] > tbl[x]: break
		ma = max(ma, tbl[x]+tbl[y])
	print(tbl[z]-ma)

Submission status: Accepted

Solution code:
def my_input():
    inp = int(raw_input())
    result = []
    while inp != 0:
        result.append(inp)
        inp = int(raw_input())
    return result

def calc1(v):
    return min([calc2(v ** 3 - x ** 3) for x in range(1,v)])

def calc2(v):
    return v - int(v ** (1.0/3)) ** 3

def main():
    for x in [calc1(i) for i in my_input()]:
        print x

main()

Submission status: Accepted

Solution code:
import math

while(1):
    z=int(raw_input())
    if z==0:
        break
    else:
        k=0
        for x in range(1,z):
            y=int(pow(z**3-x**3,1/3.))
            k=max(k,x**3+y**3)
        print z**3-k

Submission status: Accepted

Solution code:
a=1/3
while 1:
    z=int(input())
    if z==0:break
    m=0
    zz=z*z*z
    for x in range(1,int(z/pow(2,a))+1):
        xx=x*x*x
        y=int(pow(zz-xx,a))
        yy=y*y*y
        m=max(m,yy+xx)
    print(zz-m)

Submission status: Accepted

Solution code:
a=1/3
while 1:
    z=int(input())
    if z==0:break
    m,zz=0,z*z*z
    for x in range(1,int(z/pow(2,a))+1):
        xx=x*x*x
        y=int(pow(zz-xx,a))
        yy=y*y*y
        m=max(m,yy+xx)
    print(zz-m)

Submission status: Accepted

Solution code:
while 1:
    z=int(input())**3
    if z==0:break
    m,x=0,0
    while 1:
        x+=1
        xx=x**3
        y=int((z-xx)**(1/3))
        if y<x:break
        m=max(m,y**3+xx)
    print(z-m)

Submission status: Accepted

Solution code:
from bisect import bisect
L=[0]*1112
y=[pow(i,3) for i in range(1,1111)]
for z in range(1,1112):
    z3=pow(z,3)
    maxInt=0
    for x in range(1,z):
        x3=pow(x,3)
        diff=z3-x3
        y3=y[bisect(y,diff)-1]
        maxInt=max(maxInt,x3+y3)
    L[z]=z3-maxInt
while True:
    z=input()
    if z==0:break
    print L[z]

Submission status: Accepted

Solution code:
import itertools,bisect
cubes = [n**3 for n in xrange(1,1111)]
num = []
for n,m in itertools.combinations_with_replacement(cubes,2):
    num.append(n+m)
num.sort()
while True:
    z_cube = input()**3
    if z_cube == 0:
        break
    print z_cube-num[bisect.bisect(num,z_cube)-1]

Submission status: Accepted

Solution code:
while 1:
	n3=mn=input()**3
	if n3==0:break
	i=0
	while 1:
		i+=1
		i3=i**3
		j=int((n3-i3)**(1/3.0))
		if j<i:break
		m=n3-i3-j**3
		if m<mn:mn=m
	print mn

Submission status: Accepted

Solution code:
while 1:
	n3=mn=input()**3
	if n3==0:break
	i=0
	while 1:
		i+=1
		i3=i**3
		j3=int((n3-i3)**(1/3.0))**3
		if j3<i3:break
		m=n3-i3-j3
		if m<mn:mn=m
	print mn

Submission status: Accepted

Solution code:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((z3-x3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m

Submission status: Runtime Error

Solution code:
while 1:
	n=input()**3
	if n==0:break
	mn=n
	for i in range(1,n):
		for j in range(i,n):
			m=n-i**3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Submission status: Runtime Error

Solution code:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((n3-i3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m