Mathematics Thread

What is the hardest math you learned?

  • Calculus (specify in comments)

    Votes: 69 35.0%
  • Trigonometry

    Votes: 27 13.7%
  • (Enriched) Geometry

    Votes: 18 9.1%
  • Equations (specify)

    Votes: 7 3.6%
  • Graphing Equations (specify)

    Votes: 5 2.5%
  • Basic Algebra (specify)

    Votes: 9 4.6%
  • I can do better!

    Votes: 49 24.9%
  • What kind of math is that?!

    Votes: 13 6.6%

  • Total voters
    197
Only two mistakes:
your coefficients (including 'answer') should be strings because you're ljusting them. Also watch out. Don't add strings to get the answer cuz thats concatenating. (aka 123+ 456= 123456)

You're ljusting to 2*minLength+len answer. You know what it should be, I hope :P
import time
import math
import random
#Karatsuba alg is cool

def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]

def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
minLength = min(len(str(x))/2,len(str(y))/2)
if int(minLength) <= 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str((karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2)))
except:
answer = str(((a + b)*(c + d) - int(coefficient1) - int(coefficient2)))
return int(coefficient1.ljust((int(2*minLength)) + (len(coefficient1)),'0')) + int(answer.ljust(int(minLength) + int(len(answer)),'0')) + int(coefficient2)

k=0
##test 12000 times for errors
for awsiuhfds in range(0,1200):
ta,tb = random.randint(220,20000000),random.randint(220,20000000)
if karatsubaMulti(ta,tb)==ta*tb:
k=k+1
pass
else:
print karatsubaMulti(ta,tb)
print ta*tb
print "----"
raw_input(str(k)+" successes out of 1200.")

OMG, I'm so stoopid. Thanks Alex. I really appreciate all your help through my tedious project. I hope I didn't annoy you with my trivial questions.
 
OMG, I'm so stoopid. Thanks Alex. I really appreciate all your help through my tedious project. I hope I didn't annoy you with my trivial questions.

No problem. Next challenge is to implement the Toom-Cook algorithm!
Or even Shonhage-Strassen which still makes no sense to me, even after learning about the FFT and making pony graphs with it.
 
I hate my computer...It is now giving me noneType's AGAIN...

import time
import math
import random
#Karatsuba alg is cool

def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]

def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
print "Donut worry, calculating..."
minLength = min(len(str(x))/2,len(str(y))/2)
if int(minLength) <= 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str((karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2)))
except:
answer = str(((a + b)*(c + d) - int(coefficient1) - int(coefficient2)))
return int(coefficient1.ljust((int(2*minLength)) + (len(coefficient1)),'0')) + int(answer.ljust(int(minLength) + int(len(answer)),'0')) + int(coefficient2)

testAnswer = str(karatsubaMulti(1234,5678))
print "The product of 1234 and 5678 is " + testAnswer
print "Lol no dummy, it's " + str(1234*5678)
 
Last edited:
What python version? Your printing algorithm will have to be different if it's python 3. Otherwise, it works perfectly on my computer.

Edit:
also your program is naiving two digit numbers. turn if int(minLength) <= 1: into if int(minLength) < 1: and you'll be ok.
Of course this isn't the cause for your nonetypes...
 
Last edited:
python 2.7.4

import time
import math
import random
#Karatsuba alg is cool

def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]

def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
print "Donut worry, calculating..."
minLength = min(len(str(x))/2,len(str(y))/2)
if int(minLength) == 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str(karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2))
except:
answer = str((a + b)*(c + d) - int(coefficient1) - int(coefficient2))
return int(coefficient1.ljust(int(2*minLength)) + len(coefficient1),'0') + int(answer.ljust(int(minLength) + int(len(answer)),'0')) + int(coefficient2)

print "Multiply 567 and 459, class"
testAnswer = str(karatsubaMulti(567,459))
print "The product is " + testAnswer
print "Lol no dummy, it's " + str(567*459)

Hm, it now appears to be working, but only up to 3 digit numbers aka:999. karatsubaMulti(1000,1000) returns: "The product is None."

Edit: I at first thought my issues were because of my method of zero padding. But I commented out my ljust line and substituted with your multiplying by 10 method. But this did not resolve the error.

Edit2: Aha! So I went back and read my code after a good night's rest and I found a parentheses missing! Hopefully this will solve my issue. I think it will. Finally my alg has a chance at working:)

Edit3: Lol I just read your post and I saw my mistake that you found. Truly, thanks for putting up with my dumb questions and mistakes. It means a lot. Now, onto Toom-cook!
 
Last edited:
import time
import math
import random
#Karatsuba alg is cool

def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]

def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
print "Donut worry, calculating..."
minLength = min(len(str(x))/2,len(str(y))/2)
if int(minLength) == 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str(karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2))
except:
answer = str((a + b)*(c + d) - int(coefficient1) - int(coefficient2))
return int(coefficient1.ljust(int(2*minLength)) + len(coefficient1),'0')) + int(answer.ljust(int(minLength) + int(len(answer)),'0')) + int(coefficient2)

print "Multiply 567 and 459, class"
testAnswer = str(karatsubaMulti(567,459))
print "The product is " + testAnswer
print "Lol no dummy, it's " + str(567*459)
 
Does anyone know where I can learn group theory? I know it's probably difficult and way beyond my level of mathematics, but I just want to try and learn about it because it seems very fascinating.

For more cube related stuff, mebbe the Cube Theory part of the forum. For just general group theory wiki is always good to start.
 
Lel
Good luck trying to do toomcook, you have to work out how to pointwise interpolate efficiently...

:( yea...twill be furn tho, when I can akchually compute successfully. It'll be liek when you helped me with Karatsuba. Karatsuba seems so simple nao. I even wrote a zero padding function, that, to my knoledge, werks. Just needs to be formatted since my keyboards stoopid...

def zeroPad(s,n):
s = int(s)
return string(10**n)
#I've tested et and et werks, but I'm not shure abut efficiency...
 
:( yea...twill be furn tho, when I can akchually compute successfully. It'll be liek when you helped me with Karatsuba. Karatsuba seems so simple nao. I even wrote a zero padding function, that, to my knoledge, werks. Just needs to be formatted since my keyboards stoopid...

def zeroPad(s,n):
s = int(s)
return string(10**n)
#I've tested et and et werks, but I'm not shure abut efficiency...

I'm not sure if the exponential function you're using is efficient because you're relying on the built in one. The way I did zero padding was to simply add zeros on the end. Then again it may already be built in that multiplying by 10== adding zeroes.

Also I haven't found an interpolation method yet thats efficient enough to exceed naive multiplication.
 
I'm not sure if the exponential function you're using is efficient because you're relying on the built in one. The way I did zero padding was to simply add zeros on the end. Then again it may already be built in that multiplying by 10== adding zeroes.

Also I haven't found an interpolation method yet thats efficient enough to exceed naive multiplication.

Hm, intristing, how'd you do that? My first try had been the ljust fund, but that seemed to be too tedious to count teh len of the number after padding.

Also, off track here, in my Alg 2 class, there's a 7th grader named Alex :D At furst, I didnut kno who he was, and thot mebbe you transferred from le UK lol
 
Last edited:
Hm, intristing, how'd you do that? My first try had been the ljust fund, but that seemed to be too tedious to count teh len of the number after padding.

Also, off track here, in my Alg 2 class, there's a 7th grader named Alex :D At furst, I didnut kno who he was, and thot mebbe you transferred from le UK lol

Transferred from the UK? 7th grade? Why I never.
lol jk

Um yeah so the z.nfill thing works (a few pages back i used it)
 
Transferred from the UK? 7th grade? Why I never.
lol jk

Um yeah so the z.nfill thing works (a few pages back i used it)

But I thot zfill was only for adding zeroes on the left, unless you combined it with sumthing annoying like ljust...or are you talking bout sumthing else?
 
Back
Top