As a teacher in the morning he would teach geometry and astronomy, in the evening he would attend the Seljuq court as an adviser of Malik-Shah I, and at night he would study astronomy and complete important aspects of the Jalali calendar. Omar Khayyám's years in Isfahan were very productive ones, but after the death of the Seljuq Sultan Malik-Shah I the Sultan's widow turned against him as an adviser, and as a result, he soon set out on his Hajj or pilgrimage to Mecca and Medina. He was then allowed to work as a court astrologer, and was permitted to return to Nishapur, where he was renowned for his works, and continued to teach mathematics, astronomy and even medicine.
Political events in the 11th century played a big role in Khayyams life. The Seljuq turks invaded Southwest Asia and founded an empire that consisted of Mesopotamia, Syria, Palestine, and most of Iran. They occupied the grounds of Khorasan between 1038 and 1040 and conquered the rest of Iran. Toghril Beg the leader of the Seljuq proclaimed himself sultan at Nishapur in 1038 and entered Baghdad in 1055. This created a completely difficult and unstable military empire which wasn't so smooth with religion where Khayyam grew up. In this empire students would need connections to get a stable position in a job. He described this challenge in the Treatise on Demonstration of Problems of Algebra.
However he was an outstanding mathematician and astronomer and despite his challenges while growing up he wrote several works; Problems of Arithmetic, a book on music, and one on algebra before he was 25 years old. in 1070 he moved to Samarkand in Uzbekistan. one of the oldest cities in Central Asia. He was supported by Abu Tahir a prominent jurist of Samarkand and this allowed him to write his most famous work, the Treatise on Demonstration of Problems of Algebra.
In 1073 he was invited by Malik Shah, the ruler of Esfahan which was the pronounced capital by Toghril Beg, his grandfather to set up an observatory. For the next 18 years Khayyam and other respected astronomers were brought to the observatory and produced outstanding work during a period of peace which allowed Khayyam the opportunity to devote himself entirely to his work.
He led work on compiling astronomical tables and contributed to the reform of the calendar in 1079. using the observatory, he observed and measured the length of the year as 365.24219858156 days. This is a huge feat because we know that a leap year occurs because each year is 365.25 days and when added up it