# Project Euler 795 - Alternating GCD Sum

Note: My code takes ~220 sec to run, but I'm happy with my solution! # Thought Process

As usual I tried my luck on OEIS and found https://oeis.org/A353909 which gives a formula on how to calculate g(n) The core function is described here, however I am unhappy with this as in the description of the OEIS it specifically links to Project Euler Problem 795 which kind of gives away that I should stick with this formula (Although there were other solutions).

The implementation of this formula is extremely simple if you have the prime factorization of all the d's which divide n, the only tricky part is to make that part efficient which I did by using a smallest prime factor sieve and quickly building a table with the prime factorization of every number from 1 to n.

After that we just apply the formula (Lots of simplification can be made if you look carefully). Now that the problem is done, with a sour taste left in my mouth I moved on to the much more challenging and fun part for me, proving why this identity is true!

Under the Wikipedia GCD Properties section there is an interesting one which we will be using Now let's continue Most of the hard work is done now, we just look (as the original identity suggests) at the 2 cases where n is even or odd. I start with the easier one first, n is odd case And lastly the meatiest part, the case where n is even This was very fun!

If you made it this far, take this as a reminder that no one is forcing you to do Project Euler, if you solved a problem and you're left feeling unsatisfied take the time to explore your solution or others on the thread more and learn from it!

# Interactive Code

Input an integer (yourinput)

Code will output G(yourinput)
{"ft_lang_label":"__label__en","ft_lang_prob":0.944973,"math_prob":0.7641733,"size":1795,"snap":"2023-40-2023-50","text_gpt3_token_len":403,"char_repetition_ratio":0.098827474,"word_repetition_ratio":0.0,"special_character_ratio":0.2194986,"punctuation_ratio":0.06703911,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9706365,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-01T11:32:26Z\",\"WARC-Record-ID\":\"<urn:uuid:4229888a-37e2-4b57-a558-d08e8e3e271b>\",\"Content-Length\":\"564707\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:48c1a9d5-2e70-4981-9bab-f7dbc0decc68>\",\"WARC-Concurrent-To\":\"<urn:uuid:e70524dd-13e4-4ded-a67c-79d94d7fdcf1>\",\"WARC-IP-Address\":\"142.251.111.121\",\"WARC-Target-URI\":\"https://www.ivl-projecteuler.com/overview-of-problems/30-difficulty/problem-795\",\"WARC-Payload-Digest\":\"sha1:H4SYUVDU3ZCCTXQNER3XVAQACOEMYMMO\",\"WARC-Block-Digest\":\"sha1:5H7IP7ZE3I332YUAHB7LM5A4MFJQPUBG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510888.64_warc_CC-MAIN-20231001105617-20231001135617-00163.warc.gz\"}"}