@TokOut That’s all you can do to draw a graph, connect points. Since you can only plot a point at a full value (1, 2, 3, etc.) or at a half value (1.5, 2.5, 3.5, etc), you can only round to those values. But that doesn’t mean you can’t zoom in at a certain section of a graph which would allow you to plot in better detail as you zoom in.
@dave1707. I see. But e.g. is a function 3x^3 + 2x - 10 = y
The only way to select intersects with y=0 is to find such x that equal 0 in this case. We can do it only with solving it as equations. This gives me the idea of creating a project to solve equations. But still, how would we do it?
@TokOut Here’s a graphing example for your equation above. Reducing the step value will give you a better resolution of where the graph crosses x=0. Move the xx and yy sliders to move the circle cursor to show the graph crosses at x=0 and y=-10. You can also change the X and Y zoom values. This isn’t perfect but just a quick example to show you how to get better resolutions for a graph.
EDIT: Using the round cursor, you can see that when x=0, then y=-10. When y=0 then x=1.35 or close to it.
function setup()
parameter.number("step",.01,1,.3)
parameter.integer("Xzoom",1,200,150)
parameter.integer("Yzoom",1,200,25)
parameter.number("xx",-3,3,0)
parameter.number("yy",-15,1,0)
print("move Output window down")
end
function draw()
background(40, 40, 50)
fill(255)
translate(WIDTH/2,HEIGHT*.8)
stroke(255)
strokeWidth(1)
line(-500,0,500,0)
line(0,-1000,0,1000)
for x=-50,50,step do
y=3*x^3 + 2*x - 10
point(x*Xzoom,y*Yzoom)
end
noFill()
ellipse(xx*Xzoom,yy*Yzoom,20)
end
function setup()
-- math.maxinteger: 9223372036854775807
num1 = BigInt("11")
num2 = BigInt("-22")
print(num1:get() .. " + " .. num2:get() .. " = " .. num1:add(num2):get())
end
BigInt = class()
function BigInt:init(value)
self.isNegative = false
if string.sub(value, 1, 1) == "-" then
value = string.sub(value, 2, #value)
self.isNegative = true
end
self.value = value or "0"
end
function BigInt:get()
local str = self.value
if self.isNegative then
str = "(-" .. str .. ")"
end
return str
end
function BigInt:add(num)
local a = string.reverse(self.value)
local b = string.reverse(num.value)
local c = {}
-- Overwrite "c" as "a"
for x = 1, #a do
local n = 1
if a.isNegative then
n = -1
end
table.insert(c, math.tointeger(string.sub(n*a, x, x)))
end
-- Add "b" to "c"
for x = 1, #b do
if c[x] then
if b.isNegative then
c[x] = c[x] - math.tointeger(string.sub(b, x, x))
else
c[x] = c[x] + math.tointeger(string.sub(b, x, x))
end
else
if b.isNegative then
c[x] = - math.tointeger(string.sub(b, x, x))
else
c[x] = math.tointeger(string.sub(b, x, x))
end
end
end
-- Fix to digits only
local n = 0
for a, b in ipairs(c) do
b = b + n
n = 0
while b > 9 do
b = b - 10
n = 1
end
c[a] = b
end
-- Reverse Back
local p = {}
for a, b in ipairs(c) do
table.insert(p, 1, b)
end
c = p
-- Return string
local str = ""
for a, b in ipairs(c) do
str = str .. b
end
local ret = BigInt(str)
if a.isNegative then
ret.isNegative = true
end
return ret
end
Hello, I need very large integers. I’ve created a class BigInt which can add numbers. How do I subtract? I’ve got a problem with my logic…
About the primes, iI don’t know if this helps, but instead of checking every second number you could check the number below and above of multiples of six, since every prime except 2,3 is either in the form of: 6x+1 or 6x-1
@GR00G0 Kind if, true, Yes, it can be proven with the following: 2, 3, 4 are not possible because either :2 or :3, and 6x-5 = 6(x-1)+1. Nice found!
@TokOut Here’s something I have to add or subtract large strings of numbers.
function setup()
v1="465357876532246799765422346888754334579976582"
v2="855545689842234567908765434456898765333452167"
add(v1,v2)
sub(v1,v2)
end
function add(s1,s2)
local s1p,s2p=s1,s2
local s1=string.rep("0",math.max(#s1,#s2)-#s1)..s1
local s2=string.rep("0",math.max(#s1,#s2)-#s2)..s2
local s3=""
local c,v=0,0
for z=#s1,1,-1 do
local v1=tonumber(string.sub(s1,z,z))
local v2=tonumber(string.sub(s2,z,z))
v=v1+v2+c
c=0
if v>9 then
v=v-10
c=1
end
s3=v..s3
end
if c>0 then
s3="1"..s3
end
print(s1p.." + "..s2p.." = "..s3)
end
function sub(s1,s2)
local s1p,s2p=s1,s2
local s1=string.rep("0",math.max(#s1,#s2)-#s1)..s1
local s2=string.rep("0",math.max(#s1,#s2)-#s2)..s2
if s2>s1 then
minus=true
end
local s3=""
local c,v=0,0
for z=#s1,1,-1 do
local v1=tonumber(string.sub(s1,z,z))
local v2=tonumber(string.sub(s2,z,z))
if minus then
v=v2-v1-c
else
v=v1-v2-c
end
if v<0 then
v=v+10
c=1
end
s3=v..s3
end
for z=1,#s3 do
if string.sub(s3,z,z)~="0" then
s3=string.sub(s3,z)
break
elseif z==#s3 then
s3="0"
end
end
if minus then
print(s1p.." - "..s2p.." = -"..s3)
else
print(s1p.." - "..s2p.." = "..s3)
end
end
Tables of digits would be much better than strings for big integers. Here’s a lua-only implementation that looks like it could be dropped into a Codea project: https://github.com/empyreuma/bigint.lua/blob/master/bigint.lua
@LoopSpace I agree that tables would be faster. I tried 2 20,000 digit strings and it did both the add and sub functions in 1 second. Maybe I’ll convert the above program to use tables and see what the increase is.
@LoopSpace I changed the code on my iPad to use tables instead of strings and it ran 13 times faster. The input is still a string that get converted to a table. The add and sub functions use tables. I’ll change the above code later.
Does anybody use goto
?
I did, but I haven’t done so in a long time.
Here’s some code to multiple to strings of numbers. This is set up to multiply 2 1600 digit numbers.
function setup()
a="98765432109876543210987654321098765432109876543210" -- string of 50 digits
for z=1,5 do
a=a..a -- create string of 1600 digits
end
b=a
str=mult(a,b) -- multiply 2 1600 digit numbers
print(str)
print("size of a "..#a)
print("size of b "..#b)
print("size of a*b "..#str)
end
function mult(str1,str2)
local r,n1,n2={},{},{}
for z=1,#str1 do
table.insert(n1,tonumber(string.sub(str1,z,z)))
table.insert(r,0)
end
for z=1,#str2 do
table.insert(n2,tonumber(string.sub(str2,z,z)))
table.insert(r,0)
end
for x=#n2,1,-1 do
local carry=0
for y=#n1,1,-1 do
local v=r[x+y]+n2[x]*n1[y]+carry
r[x+y]=v%10
carry=math.floor(v/10)
end
r[x]=carry
end
if r[1]==0 then
table.remove(r,1)
end
return(table.concat(r))
end