Hello to all!
I am a newcomer in the Nim programming language. My experience in programming, so far, includes:
Now, C has always been something like a chimera -due to its cryptic character- so I decided (many days back) to try to learn Nim -using various resources (here and there). It is meant, of course, that I am self taught, and my "arsenal" is a (very) good knowledge of higher Mathematics (PhD in Theoretical Physics) -but theory of Algorithms is not included.
To my questions now. I have written a, perhaps naive, piece of code to calculate the nth Fibonacci number:
var
nth = 91
fnm2 = 0
fnm1 = 1
fn : int
echo(fnm2)
for i in 0 ..< nth:
fn = fnm2 + fnm1
fnm2 = fnm1
fnm1 = fn
echo(fnm2)
The strange (to me, of course) is that this program can calculate only(!) up to 91st (including) Fibonacci number. The corresponding C program, using long long, could go up to 94th. Using Otus Lisp (even recursively!) I can go beyond 100th -ditto for Mathematica.
Question #2: How could I increase the upper limit, in order to access higher terms in the Fibonacci sequence?
As a closing yet another, typical (I suppose) question. Thus far, my learning resources are (in order of increasing difficulty and thematic areas):
So,
Question #3: before deciding to invest in the official book, is there any other suggestion?
obviously, if the initial steps turn out to be a smooth sailing I will try to buy the new version (2.0) by AR, but it is too soon for now.
I would like to thank you all in advance for both your time and help. Please, forgive the naive character of my questions.
sizeof(int) is same to pointer size in Nim. That means int is 64bit on 64bit CPU and is 32bit on 32bit CPU. int in Nim is signed int. If you use uint or uint64, you can use larger value but NO overflow check.
If you need larger size int type, https://github.com/nim-lang/bigints https://github.com/status-im/nim-stint
If you run Nim with --listCmd option, you can see Nim calls backend C compiler. You can read Nim generated C/C++ code in nimcache directory: https://nim-lang.org/docs/nimc.html#compiler-usage-generated-c-code-directory
Thank you very much. Unfortunately, my knowledge of C is not sufficient to appreciate the topic regarding the second link you mentioned.
So, bottom line is that there is no native way to go further in the sequence without the library you mentioned in the first link, right?
Again, thank you.
int is a fixed number of bits in nim. You might want to use a type like bigints. Start by getting:
import bigints
to work.That is the nature of the beast, your int64 is pretty much running out of space to represent a Fibonacci number that large (and ends a bit earlier in nim because the fact nim ints are signed by default means defacto it's 63 bit in order to be able to represent negative numbers. Got to use uint64 to get the same number as C).
As stated koistinen, you'll need a type that interprets a larger area of memory as an int, e.g. 128 bit, so that you can represent larger fibonacci numbers. Bigints are such types that are available as nim packages.
After, installing choosenim and latest stable nim and bigints I tried: import bigints
var
nth : int = 500
fnm2 : BigInt = initBigInt(0)
fnm1 : BigInt = initBigInt(1)
fn : Big_int
echo(fnm2)
for i in 0 ..< nth:
fn = fnm2 + fnm1
fnm2 = fnm1
fnm1 = fn
echo(fnm2)
which appears to work fine. Installing with choosenim was really convenient compared to when I last installed nim and using nimble it was just a matter of nimble install bigints to get the library.I had read in various sites/articles about the "hostility" in the Nim forum, in case someone expresses a different opinion. I did not want to believe it. Sadly enough I just realised that it is true; 2 sad cases: alexeypetrushin and SolitudeSF.
Well, THIS is a more important reason for me to abandon NIM. Congrats guys (to mimic your irony).
PS. I really thank the rest, KIND persons, which POLITELY answered my questions thus far.
I think that it is very sad that we lose people that are attracted to nim by its (very strong!) technical merits because of the way we respond to them in this forum. Some might think that the replies that offended Odysseus were that bad, but it's undisputable that they were not the friendliest to a newcomer that has shown interest in learning nim. If on top of that you add the (perhaps undeserved) bad reputation that the nim community has, I can understand why Odysseus would choose to move on and do something else.
Honestly, I think we can and should do better.