Need help with semiperfect numbers

local function SemiPerfect(Number, Table)
	for i = 1, #Table, 1 do
		local Temp = table.remove(Table, i)
		if SemiPerfect(Number - Temp, Table) then
			return true
		table.insert(Table, i, Temp)
	return false

I have this semiperfect number check and it is working, but it is very very slow when using bigger numbers, I can’t add a task.wait() to prevent lag because that makes it take a so long to even solve 66

btw the second parameter is the factors for the first parameter in descending order

this is for my UltiMath module and I am currently trying to add this and a weird number check, I don’t want to make the code bad

thanks for helping if you did

EDIT: I removed a bad if statement in the code

1 Like

the post by Nipokkio says that their solution is extremely fast
but I can’t seem to implement it, thought I would show this to help out

EDIT: gonna try Nipokkio’s method later, but I am stumped on how to find the dSum parameter, that is what I am confused on right now, this is because of that variable named divsIndex
I don’t even know what divsIndex is supposed to be, I am guessing it stands for divisors index for the factors table, just don’t know what it is equal to

ok so I found out a couple of problems here
ignore Nipokkio’s method from the last reply for now, because I know why this is happening sort of
basically adding a yield after for i = 1, #Table, 1 do won’t return anything at all, which is why 66 doesn’t work with that task.wait()

the other problem is the reason why this method “doesn’t work with big numbers” is because of this line if SemiPerfect(Number - Temp, Table) then, this obviously due to recursion
all numbers 1 - 299 work and any number above that depending on the number, will error on that line as a script timeout

my only option is to figure out how to yield without stopping all code or use Nipokkio’s method

I am trying to figure this out

it might depend on the number because of the amount of factors each number has, one might have more then the other

like how entering 300 errors, (300 has 17 factors not including the number 300)
and how entering 301 errors, (301 has 3 factors not including the number 301)
here is a factor table for both of those numbers, yea I think the number of factors is why it is giving a script timeout, too many recursions are running at once without any yielding

but why won’t it let me add any yielding without not returning anything what so ever, so odd
maybe it is something to do with the recursion?

looks like I figured this out, basically it does work while yielding, but recursion happens much slower when yielding at all

gotta love scripting…

so I just remade the whole function without recursion and instead of stopping at 300(18 factors)
this one can do numbers up to any number, keep in mind it depends on how many factors you have, 100 factors takes like ~8 seconds if I remember right
so it is obviously better

I am going to make the GetFactors() function more efficient later by using prime factorization which would greatly shorten the run time of SemiPerfect()