Isaac Newton was a legendary scientist best known for establishing the laws of gravity. But in his free time, he dabbled in experiments worthy of Voldemort.

Like the “Harry Potter” villain, Newton actively sought the philosopher’s stone, a mystical tool that allows mere mortals to turn any object into silver or gold, and has the power to grant eternal life.

In his new book, “Isaac Newton: The Asshole Who Reinvented The Universe,” author Florian Freistetter depicts Newton as a thoughtless genius with no social skills and a harsh demeanor who, despite his scientific acumen, was also devoted to alchemy.

“Alchemy was . . . not merely a hobby [for Newton],” he writes. “If anything, it would be closer to the truth to call Newton’s research into physics a ‘hobby’ that he fitted in between his theological and alchemistic studies.”

Newton was so obsessed with this hocus pocus that he built his own chimneys and furnaces for conducting experiments. He considered the pursuit of alchemy so sacred, he never spoke about it in public, writing about it only in code.

Here’s one example of his writings:

“This red powder is accordingly Flamel’s male wingless dragon, for after it has been extracted from its normal powder, it is one of the three substances out of which the bath of sun and moon is made.”

Newton kept his alchemical recipes deliberately confusing. His recipe for a philosopher’s stone that supposedly multiplied gold contained ingredients like “fiery dragons,” “doves of Diana” or “eagles of mercury.”

“One might expect to find such words in muddled texts by would-be medieval magicians. Yet they are by Newton,” Freistetter writes.

“Newton saw alchemy as just another way to understand the universe,” Freistetter added in an interview with The Post. “He was convinced that the true god is present everywhere, not only in spiritual form but also in substance. Thus, there had to be some sort of ‘divine matter’; some fundamental part of matter that would allow the transformation from everything into everything. For Newton . . . the study of alchemy was as important and serious for him as . . . science.”

There were signs early on that Newton, born in January 1643 in England, would not take a traditional path through life.

“Newton was a strange and awkward chap from the very beginning,” Freistetter writes in the book. “‘What shall become of me? I will make an end of it. I can only weep. I do not know what to do.’ He wrote these depressing lines in his notebook while still a youth.”

With his father dead before his birth and his remarried mother sending him off to live with grandparents, Newton, smarter than his peers, lived an isolated young life.

A diary he wrote at 19 contained many entries typical for a young man his age — admitting he was guilty of “peevishness with his mother,” for example — but also lines about “wishing death and hoping it to some” and “threatening my father and mother Smith to burn them and the house over them.”

As he embarked on a scientific career, he sacrificed way more than most in the search for ultimate truth. “Right from the beginning, Isaac Newton ticked all the boxes as a mad professor,” Freistetter writes, noting that the young scientist slept three or four hours a night and barely ate.

“As a young man, he stuck a needle in his eye in order to find out more about the nature of light . . . Newton stuck the thing between his eyeball and the socket in such a way that he could press the eyeball from behind with the tip of the needle. He was thus able to change the shape of the eyeball and observe the effect this had on his visual perception. He saw rings of differing sizes and colors that changed when he moved the needle but disappeared when it was still.” This was just one risk Newton took with his eyes; he also stared as long and hard as he could into a solar eclipse to determine the effect on eyesight.

“He survived all of these self-experiments without losing his sight and was deeply fascinated by the effects that arose,” Freistetter wrote. “If he stared at the sun for long enough, he could later still see after-images and curious colors that were obviously not real. Despite the interesting fruits of his research, Newton was actually slightly concerned about his eyes and locked himself away in a completely darkened room for three days, only reemerging when they were working normally again.”

Meanwhile, Newton got deeper into alchemy, choosing the pseudonym Jehovah Sanctus Unus — or “holy god” — to use among his fellow practitioners. He even dared to predict when the world might end.

Based on extensive studies of biblical texts, he estimated that the world would “reset” in 2060, when “the Kingdom of God” would prevail on the Earth, Freistetter writes. Meanwhile, Newton castigated other doomsday prophesiers for foretelling a more imminent apocalypse.

“It may end later, but I see no reason for its ending sooner,” Newton boldly proclaimed.

“Newton spent a great deal of time with the study of religious texts and tried to build a chronology of past events to get all those stories sorted and into . . . order,” Freistetter says. “He was convinced that future events were already ordained by god. From the Bible, Newton extracted some ‘prophetic’ time periods. For him, 2060 [would be] a new beginning; maybe accompanied by war and catastrophes but ultimately the start of a new divine era.”

Newton died in March 1727. While his fascination with alchemy shows a slightly loopy side of the scientist, Freistetter says it shouldn’t detract from his reputation — or his legacy — as one of the world’s greats.

“Newton was a genius like no other,” Freistetter says. “No man before or after, with the possible exception of Einstein, has changed our knowledge and view of the world so fundamentally as Isaac Newton. He showed us that the world is understandable; that there are such things as natural laws and that they can be formulated in a precise, mathematical fashion. He made modern science possible.”