Handling numbers larger than Long in VBA
Asked Answered
G

2

9

I am Currently trying to write some code in VBA to solve a problem from Project Euler. I have been trying to answer a question that requires you to find primes that can be divided into a number that will not fit in a long. Any suggestions as how to handle this problem?

I know I can split the number between two variables and I have done that for addition and subtraction but never division. Any help will be appreciated.

Glassco answered 3/12, 2009 at 15:31 Comment(4)
Basically you're supposed to store the numbers as text and create functions for basic math operations as adding, subtracting, etc. For more information and implementation, take a look here: Large Number ArithmeticRusson
primes that are divisible by a number...?Pigeonhole
I think we know what he meant @Glassco primes are not divisible by any number but 1 and itself. You can actually test this by looping up until square root of a number. You can probably be even faster then that, I know you shouldn't have to go to half of that number (division, so the square root is a bit faster).Elyot
Sorry for the misswording, I have updated the question to make it more clear.Glassco
M
9

You can define a Decimal data type (12 Bytes), but only within a variant.

Dim i As Variant

i = CDec(i)

Mordant answered 6/10, 2016 at 8:3 Comment(0)
F
-6

Use the "Double" data type that takes larger values.

Fossette answered 18/11, 2013 at 14:58 Comment(1)
Using a "Double" is a bad idea as it's limited to a set number of significant figures so you'll lose accuracy with larger numbers.Bottali

© 2022 - 2024 — McMap. All rights reserved.