├── C++ ├── problem_1.cpp ├── problem_2.cpp ├── problem_3.cpp ├── problem_4.cpp └── problem_5.cpp ├── README.md ├── java ├── Problem_10.java ├── Problem_11.java ├── Problem_12.java ├── Problem_13.java ├── Problem_2.java ├── Problem_23.java ├── Problem_3.java ├── Problem_35.java ├── Problem_4.java ├── Problem_42.java ├── Problem_5.java ├── Problem_55.java ├── Problem_6.java ├── Problem_7.java ├── Problem_8.java ├── Problem_9.java ├── problem_1.java ├── problem_16.java ├── problem_17.java ├── problem_20.java └── problem_25.java ├── python ├── problem_1.py ├── problem_10.py ├── problem_11.py ├── problem_12.py ├── problem_13.py ├── problem_14.py ├── problem_15.py ├── problem_16.py ├── problem_17.py ├── problem_18.py ├── problem_19.py ├── problem_2.py ├── problem_20.py ├── problem_21.py ├── problem_22.py ├── problem_23.py ├── problem_24.py ├── problem_25.py ├── problem_26.py ├── problem_27.py ├── problem_3.py ├── problem_30.py ├── problem_301.py ├── problem_31.py ├── problem_34.py ├── problem_37.py ├── problem_4.py ├── problem_45.py ├── problem_5.py ├── problem_50.py ├── problem_51.py ├── problem_52.py ├── problem_53.py ├── problem_55.py ├── problem_57.py ├── problem_58.py ├── problem_6.py ├── problem_7.py ├── problem_75.py ├── problem_8.py ├── problem_9.py └── problem_91.py └── rust ├── problem_1.rs └── problem_2.rs /C++/problem_1.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. 4 | The sum of these multiples is 23. 5 | Find the sum of all the multiples of 3 or 5 below 1000. 6 | 7 | *******************************************************************************/ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int sum = 0; 16 | for(int i = 1;i<1000;i++){ 17 | if(i%3==0||i%5==0){ 18 | sum = sum + i; 19 | } 20 | } 21 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int limit = 4000000; 7 | int a = 1, b = 1, sum = 0,temp; 8 | while (b < limit){ 9 | if (b % 2 == 0){ 10 | sum = sum + b; 11 | } 12 | temp = a + b; 13 | a = b; 14 | b = temp; 15 | } 16 | cout << sum; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /C++/problem_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int bigNum = 600851475143; 7 | int factor = 2, lastFactor = 1; 8 | while(bigNum>1){ 9 | if(bigNum%factor==0){ 10 | lastFactor = factor; 11 | bigNum = bigNum / factor; 12 | while (bigNum % factor==0) 13 | bigNum = bigNum / factor; 14 | } 15 | factor = factor + 1; 16 | } 17 | cout << lastFactor; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C++/problem_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int reverse(int n){ 5 | int reversed = 0; 6 | while (n > 0){ 7 | reversed = (10 * reversed) + (n % 10); 8 | n = n / 10; 9 | } 10 | return reversed; 11 | } 12 | bool isPalindrome(int n){ 13 | return (n == reverse(n)); 14 | } 15 | 16 | int main(){ 17 | int largestPalindrome = 0; 18 | int a = 100; 19 | while (a <= 999){ 20 | int b = 100; 21 | while(b<=999){ 22 | if (isPalindrome(a*b) && a*b > largestPalindrome) 23 | largestPalindrome = a * b; 24 | b = b + 1; 25 | } 26 | a = a + 1; 27 | } 28 | cout << largestPalindrome; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /C++/problem_5.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | 6 | int LCM(int n1, int n2) { 7 | int lcm = 0; 8 | for (int i = 1; i < n2; i++) { 9 | lcm = (n1 > i) ? n1 : i; 10 | for (lcm = (n1 > i) ? n1 : i; lcm <= (n1*n2); lcm++) 11 | { 12 | if(lcm % n1 == 0 && lcm % i == 0) 13 | { 14 | n1 = lcm; 15 | break; 16 | } 17 | lcm += 1; 18 | } 19 | } 20 | return lcm; 21 | } 22 | 23 | int main() 24 | { 25 | int res=LCM(20,100); 26 | cout< 6 | 7 | This repo contains solutions from [ProjectEuler](https://projecteuler.net/). 8 | You can solve any unsolved solution in this repo from [ProjectEuler Archive](https://projecteuler.net/archives). 9 | 10 | ## Note: Don't send PR of an answer if its already there. Optimizations are allowed 11 | 12 | ## Steps To Contribute 13 | 14 | - Fork The Repo 15 | - Find a question that has not been solved yet or has a better solution. 16 | - Solve the question make sure you get the expected answer. 17 | - Put your code with name `problem_number.extension` in respective folder of the language used. 18 | Example: If you are solving 15th problem in JAVA, place your file in java directory and name it `problem_15.java` 19 | Note: The filename is case-sensitive so always use lowercaps in file name. 20 | - Push it to your repo and send a PR. 21 | 22 | Once we validate it, we shall accept your pull request. 23 | 24 | Lets Have Fun in This HacktoberFest!! 25 | 26 | ## Solutions 27 | 28 | Solutions in [Python](https://github.com/TheSpeedX/codex/tree/master/python) 29 | 30 | Solutions in [Java](https://github.com/TheSpeedX/codex/tree/master/java) 31 | 32 | Solutions in [Rust](https://github.com/TheSpeedX/codex/tree/master/rust) 33 | 34 | Solutions in [C++](https://github.com/TheSpeedX/codex/tree/master/C%2B%2B) 35 | 36 | New Languages Are Welcome... 37 | -------------------------------------------------------------------------------- /java/Problem_10.java: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. 3 | Find the sum of all the primes below two million. 4 | 5 | */ 6 | class Problem_10 { 7 | 8 | public static boolean isPrime(long n) 9 | 10 | { 11 | if (n < 2) {return false;} 12 | else if (n == 2) {return true;} 13 | for (int a = 3; a < Math.sqrt(n) + 1; a += 2) 14 | { 15 | if (n % a == 0) 16 | { 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | 24 | public static void main(String[] args) 25 | { 26 | long s = 2; 27 | int b; 28 | for(b = 3; b < 2000000; b += 2) 29 | { 30 | if(isPrime(b)== true) 31 | { 32 | s = s + b; 33 | } 34 | } 35 | 36 | System.out.println(s); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java/Problem_11.java: -------------------------------------------------------------------------------- 1 | class Problem_11 { 2 | public static void main(String[] args){ 3 | int[][] grid = new int[][]{ 4 | {8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8}, 5 | {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0}, 6 | {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65}, 7 | {52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91}, 8 | {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80}, 9 | {24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50}, 10 | {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70}, 11 | {67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21}, 12 | {24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72}, 13 | {21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95}, 14 | {78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92}, 15 | {16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57}, 16 | {86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58}, 17 | {19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40}, 18 | {4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66}, 19 | {88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69}, 20 | {4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36}, 21 | {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16}, 22 | {20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54}, 23 | {1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48} 24 | }; 25 | int largest = 0; 26 | 27 | for(int i = 0; i < grid.length; i++) { 28 | for(int j = 0; j < grid[i].length; j++) { 29 | if(j < grid[i].length - 3) { 30 | int pRight = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; 31 | if(pRight > largest) { 32 | largest = pRight; 33 | } 34 | } 35 | if(i < grid.length - 3) { 36 | int pDown = grid[i][j] * grid[i + 1][j] * grid[i + 2][j] * grid[i + 3][j]; 37 | if(pDown > largest) { 38 | largest = pDown; 39 | } 40 | 41 | if(j < grid[i].length - 3) { 42 | int pDiag = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; 43 | if(pDiag > largest) { 44 | largest = pDiag; 45 | } 46 | } 47 | if(j > 3) { 48 | int pODiag = grid[i][j] * grid[i + 1][j - 1] * grid[i + 2][j - 2] * grid[i + 3][j - 3]; 49 | if(pODiag > largest) { 50 | largest = pODiag; 51 | } 52 | } 53 | } 54 | } 55 | } 56 | System.out.println(largest); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /java/Problem_12.java: -------------------------------------------------------------------------------- 1 | /* 2 | The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 3 | The first ten terms would be: 4 | 5 | 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... 6 | 7 | Let us list the factors of the first seven triangle numbers: 8 | 9 | 1: 1 10 | 3: 1,3 11 | 6: 1,2,3,6 12 | 10: 1,2,5,10 13 | 15: 1,3,5,15 14 | 21: 1,3,7,21 15 | 28: 1,2,4,7,14,28 16 | We can see that 28 is the first triangle number to have over five divisors. 17 | 18 | What is the value of the first triangle number to have over five hundred divisors? 19 | */ 20 | 21 | class Problem_12{ 22 | /* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */ 23 | public static int triangleNumber(int n) { 24 | int sum = 0; 25 | for (int i = 0; i <= n; i++) 26 | sum += i; 27 | return sum; 28 | } 29 | public static void main(String[] args) { 30 | int j = 0; 31 | int n = 0; 32 | int noOfDiv = 0; 33 | while (noOfDiv<= 500) { //checks if no of divisors<=500 34 | noOfDiv= 0; 35 | j++; 36 | n = triangleNumber(j); 37 | // checking for divisors and counting them untill sqrt(n) 38 | for (int i = 1; i <= Math.sqrt(n); i++) 39 | if (n % i == 0) 40 | noOfDiv++; 41 | // 1 to the square root of the number holds exactly half of the divisors 42 | // so multiply it by 2 to include the other corresponding half 43 | noOfDiv*= 2; 44 | } 45 | System.out.println(n); 46 | } 47 | } -------------------------------------------------------------------------------- /java/Problem_13.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | 3 | class Main 4 | { 5 | public static void main(String[] args) 6 | { 7 | long begin = System.currentTimeMillis(); 8 | String data[] = { "37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538", "74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250", "23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757", "28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318", "47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951", "62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178", "92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828", "80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586", "86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938", "54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145", "36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045", "17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692", "51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691", "15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126", "18336384825330154686196124348767681297534375946515","80386287592878490201521685554828717201219257766954", "78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984", "48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332", "59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085", "41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375", "35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275", "88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526", "36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141", "91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470", "23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470", 9 | "63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822", "95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236","37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522", "29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715", "38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539", "40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903", "41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457", "23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672", "11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581", "97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327", "55121603546981200581762165212827652751691296897789", 10 | "32238195734329339946437501907836945765883352399886", "75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731", 11 | "32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622", "73267460800591547471830798392868535206946944540724", 12 | "76841822524674417161514036427982273348055556214818","97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090", 13 | "10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039", "62184073572399794223406235393808339651327408011116", 14 | "66627891981488087797941876876144230030984490851411", "60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096", "66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252", "16730939319872750275468906903707539413042652315011", 15 | "94809377245048795150954100921645863754710598436791", "78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078", "40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535", "41503128880339536053299340368006977710650566631954", 16 | "81234880673210146739058568557934581403627822703280", "82616570773948327592232845941706525094512325230608","22918802058777319719839450180888072429661980811197", "77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516", "20849603980134001723930671666823555245252804609722", 17 | "53503534226472524250874054075591789781264330331690" }; 18 | 19 | BigInteger answer = BigInteger.ZERO; 20 | for (int i = 0; i < data.length; i++) 21 | { 22 | BigInteger b = new BigInteger(data[i]); 23 | answer = answer.add(b); 24 | } 25 | System.out.println(answer.toString().substring(0, 10)); 26 | } 27 | } -------------------------------------------------------------------------------- /java/Problem_2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 3 | 4 | 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 5 | 6 | By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 7 | 8 | */ 9 | class Problem_2 { 10 | 11 | public static void main(String[] args) { 12 | 13 | int x = 0; 14 | int y = 1; 15 | int z; 16 | int max = 4000000 ; 17 | int sum = 0; 18 | for (z = 0; z < max; z++) { 19 | 20 | z = x + y; 21 | x = y; 22 | y = z; 23 | 24 | 25 | if (z % 2 == 0) {sum += z;} 26 | } 27 | System.out.println(sum); 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java/Problem_23.java: -------------------------------------------------------------------------------- 1 | class Problem_23 { 2 | public static void main(String[] args){ 3 | int sum = 0; 4 | boolean[] abundant = new boolean[28124]; 5 | for(int i = 0; i < abundant.length; i++) { 6 | abundant[i] = isAbundant(i); 7 | } 8 | for(int i = 1; i < abundant.length; i++) { 9 | if(!isSumOfAbundant(abundant, i)) { 10 | sum += i; 11 | } 12 | } 13 | System.out.println(sum); 14 | } 15 | 16 | public static boolean isAbundant(int n) { 17 | int sum = 0; 18 | for(int i = 1; i < Math.floor(n / 2) + 1; i++){ 19 | if(n % i == 0) { 20 | sum += i; 21 | } 22 | } 23 | return sum > n; 24 | } 25 | 26 | public static boolean isSumOfAbundant(boolean[] abundant, int n) { 27 | for(int i = 0; i < n; i++) { 28 | if(abundant[i] && abundant[n - i]) { 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /java/Problem_3.java: -------------------------------------------------------------------------------- 1 | /* 2 | The prime factors of 13195 are 5, 7, 13 and 29. 3 | 4 | What is the largest prime factor of the number 600851475143 ? 5 | 6 | */ 7 | class Problem_3 { 8 | 9 | public static void main(String[] args) { 10 | 11 | long i = 1; 12 | long n = 600851475143l; 13 | long x = 0; 14 | 15 | for (i = 1; i <= n; i++) { 16 | 17 | if (n % i == 0) {n = n/i; x = i;} 18 | 19 | } 20 | System.out.println(x); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /java/Problem_35.java: -------------------------------------------------------------------------------- 1 | class Problem_35 { 2 | public static void main(String[] args){ 3 | int count = 0; 4 | for(int i = 2; i < 1000000; i++) { 5 | if(isPrime(i) && isCircular(i)) { 6 | count++; 7 | } 8 | } 9 | System.out.println(count); 10 | } 11 | 12 | public static boolean isPrime(int n) { 13 | if(n == 2) { 14 | return true; 15 | } 16 | if(n == 1 || n % 2 == 0) { 17 | return false; 18 | } 19 | for(int i = 3; i * i <= n; i += 2) { 20 | if(n % i == 0) { 21 | return false; 22 | } 23 | } 24 | return true; 25 | } 26 | 27 | public static boolean isCircular(int num) { 28 | String s = Integer.toString(num); 29 | for (int i = 0; i < s.length(); i++) { 30 | if (!isPrime(Integer.parseInt(s.substring(i) + s.substring(0, i)))) { 31 | return false; 32 | } 33 | } 34 | return true; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java/Problem_4.java: -------------------------------------------------------------------------------- 1 | /* 2 | A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 � 99. 3 | 4 | Find the largest palindrome made from the product of two 3-digit numbers. 5 | 6 | */ 7 | class Problem_4 { 8 | 9 | public static void main(String[] args) { 10 | 11 | int maxProd = 0; 12 | 13 | for (int i = 999; i > 99; i--) 14 | { 15 | for (int j = i; j > 99; j--) 16 | { 17 | int prod = i * j; 18 | if (prod < maxProd) 19 | break; 20 | int num = prod; 21 | int rev = 0; 22 | 23 | while (num != 0) 24 | { 25 | rev = rev * 10 + num % 10; 26 | num /= 10; 27 | } 28 | 29 | if (prod == rev && prod > maxProd) 30 | maxProd = prod; 31 | } 32 | } 33 | System.out.println(maxProd); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /java/Problem_42.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Problem_42 { 4 | public static void main(String[] args){ 5 | String[] names = new String[]{}; 6 | int count = 0; 7 | BufferedReader reader = null; 8 | 9 | try { 10 | reader = new BufferedReader(new FileReader(new File("PE042_words.txt"))); 11 | String text = null; 12 | 13 | while ((text = reader.readLine()) != null) { 14 | text = text.substring(1, text.length() - 1); 15 | names = text.split("\",\""); 16 | for(int i = 0; i < names.length; i++) { 17 | int numericValue = 0; 18 | for(int j = 0; j < names[i].length(); j++) { 19 | numericValue += (names[i].charAt(j) - 'A') + 1; 20 | } 21 | if(isTriangle(numericValue)) { 22 | count++; 23 | } 24 | } 25 | System.out.println(count); 26 | } 27 | } catch (FileNotFoundException e) { 28 | e.printStackTrace(); 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } finally { 32 | try { 33 | if (reader != null) { 34 | reader.close(); 35 | } 36 | } catch (IOException e) { 37 | } 38 | } 39 | } 40 | 41 | public static boolean isTriangle(int n) { 42 | for(int i = 0; 0.5 * i * (i + 1) <= n; i++) { 43 | if(0.5 * i * (i + 1) == n) { 44 | return true; 45 | } 46 | } 47 | return false; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /java/Problem_5.java: -------------------------------------------------------------------------------- 1 | /* 2 | 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 3 | 4 | What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 5 | 6 | */ 7 | class Problem_5 { 8 | 9 | public static int LCM(int n1, int n2) { 10 | 11 | int lcm = 0; 12 | 13 | for (int i = 1; i < n2; i++) { 14 | 15 | lcm = (n1 > i) ? n1 : i; 16 | 17 | for (lcm = (n1 > i) ? n1 : i; lcm <= (n1*n2); lcm++) { 18 | 19 | if(lcm % n1 == 0 && lcm % i == 0){ 20 | 21 | n1 = lcm; 22 | break; 23 | 24 | } 25 | 26 | lcm += 1; 27 | 28 | } 29 | } 30 | return lcm; 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | System.out.println(LCM(1, 20)); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /java/Problem_55.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | 3 | class Number { 4 | public static void main(String[] args) { 5 | long count = 0; 6 | for (long i = 1; i < 10000; i++) { 7 | if (isLychrelNumber(i)) { 8 | count++; 9 | } 10 | } 11 | System.out.println(count); 12 | } 13 | 14 | private static boolean isLychrelNumber(long number) { 15 | BigInteger next = BigInteger.valueOf(number); 16 | int numIterations = 1; 17 | do { 18 | next = next.add(new BigInteger( 19 | new StringBuffer(next.toString()).reverse().toString())); 20 | if (isPalindrome(next.toString())) { 21 | return false; 22 | } 23 | numIterations++; 24 | } 25 | while (numIterations <= 50); 26 | return true; 27 | } 28 | 29 | private static boolean isPalindrome(String word) { 30 | int length = word.length(); 31 | for (int i = 0; i < length/2; i++) { 32 | if (word.charAt(i) != word.charAt(length-1-i)) { 33 | return false; 34 | } 35 | } 36 | return true; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /java/Problem_6.java: -------------------------------------------------------------------------------- 1 | 2 | class Problem_6 { 3 | 4 | public static void main(String[] args) { 5 | 6 | int i=100; 7 | int a=(i*(i+1))/2; 8 | int b=(i*(i+1)*(2*i+1))/6; 9 | int c=a*a; 10 | System.out.println(c-b); 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /java/Problem_7.java: -------------------------------------------------------------------------------- 1 | /* 2 | By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 3 | What is the 10 001st prime number? 4 | 5 | */ 6 | class Problem_7 { 7 | 8 | public static boolean isPrime(long n) { 9 | 10 | if (n < 2) {return false;} 11 | else if (n == 2) {return true;} 12 | 13 | for (int i = 3; i < Math.sqrt(n) + 1; i+=2) 14 | { if (n % i == 0) 15 | 16 | {return false;} 17 | 18 | } 19 | return true; 20 | } 21 | 22 | 23 | public static void main(String[] args) { 24 | 25 | int a=1; 26 | 27 | for(int i=3;i>0;i= i+=2) 28 | 29 | { 30 | if(isPrime(i)==true) 31 | 32 | { 33 | a++; 34 | } 35 | 36 | if(a==10001) 37 | 38 | { 39 | System.out.println(i); 40 | break; 41 | } 42 | } 43 | 44 | } 45 | 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /java/Problem_8.java: -------------------------------------------------------------------------------- 1 | /* 2 | The four adjacent digits in the 1000-digit number that have the greatest product are 9 � 9 � 8 � 9 = 5832. 3 | 4 | 73167176531330624919225119674426574742355349194934 5 | 96983520312774506326239578318016984801869478851843 6 | 85861560789112949495459501737958331952853208805511 7 | 12540698747158523863050715693290963295227443043557 8 | 66896648950445244523161731856403098711121722383113 9 | 62229893423380308135336276614282806444486645238749 10 | 30358907296290491560440772390713810515859307960866 11 | 70172427121883998797908792274921901699720888093776 12 | 65727333001053367881220235421809751254540594752243 13 | 52584907711670556013604839586446706324415722155397 14 | 53697817977846174064955149290862569321978468622482 15 | 83972241375657056057490261407972968652414535100474 16 | 82166370484403199890008895243450658541227588666881 17 | 16427171479924442928230863465674813919123162824586 18 | 17866458359124566529476545682848912883142607690042 19 | 24219022671055626321111109370544217506941658960408 20 | 07198403850962455444362981230987879927244284909188 21 | 84580156166097919133875499200524063689912560717606 22 | 05886116467109405077541002256983155200055935729725 23 | 71636269561882670428252483600823257530420752963450 24 | 25 | Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? 26 | */ 27 | class Problem_8 { 28 | 29 | public static void main(String[] args) { 30 | 31 | String s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"; 32 | long max=0; 33 | 34 | for(int i=0;i<=s.length()-13;i++) 35 | { 36 | long p=1; 37 | for(int j=i;jmax) 46 | { 47 | max=p; 48 | } 49 | } 50 | System.out.println(max); 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /java/Problem_9.java: -------------------------------------------------------------------------------- 1 | /* 2 | There exists exactly one Pythagorean triplet for which a + b + c = 1000. 3 | Find the product abc. 4 | */ 5 | class Problem_9 { 6 | 7 | public static void main(String[] args) { 8 | 9 | for(int a = 3; a <= 1000; a++) 10 | { 11 | for(int b = a + 1; b <= 1000; b++) 12 | { 13 | double cSquare = Math.pow(a,2) + Math.pow(b,2); 14 | double c = Math.pow(cSquare,0.5); 15 | if(a + b + c == 1000) 16 | 17 | { 18 | double prod = a*b*c; 19 | System.out.println((int)prod); 20 | break; 21 | } 22 | 23 | } 24 | } 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /java/problem_1.java: -------------------------------------------------------------------------------- 1 | /*If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 2 | 3 | Find the sum of all the multiples of 3 or 5 below 1000.*/ 4 | 5 | class problem1 6 | { 7 | public static void main(String args[]) 8 | { 9 | int sum=0; 10 | for(int i=1;i<=1000;i++) 11 | { 12 | if(i%3==0 || i%5==0) 13 | sum+=i; 14 | } 15 | System.out.println("Sum is "+sum 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/problem_16.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | 3 | class problem16 { 4 | public static void main(String[]args){ 5 | BigInteger mul=new BigInteger("1"); 6 | BigInteger bg=new BigInteger("2"); 7 | 8 | for (int i = 0; i < 1000; i++) { 9 | mul=mul.multiply(bg); 10 | } 11 | 12 | int sum=0; 13 | String text=mul.toString(); 14 | for (int i = 0; i < text.length(); i++) { 15 | sum+=Integer.parseInt(new Character(text.charAt(i)).toString()); 16 | } 17 | System.out.println(sum); 18 | } 19 | } -------------------------------------------------------------------------------- /java/problem_17.java: -------------------------------------------------------------------------------- 1 | class problem17 2 | { 3 | public static String[] ones = 4 | { "","one","two","three","four","five","six","seven","eight","nine","ten", 5 | "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; 6 | 7 | public static String[] tens = 8 | {"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; 9 | 10 | public static String[] hundreds = 11 | {"","onehundred","twohundred","threehundred","fourhundred","fivehundred","sixhundred", 12 | "sevenhundred","eighthundred","ninehundred"}; 13 | 14 | public String word(int arg) 15 | { 16 | String withAnd = ""; 17 | 18 | if(arg == 1000) 19 | { 20 | return "onethousand"; 21 | } 22 | 23 | if(arg < 20) 24 | { 25 | return ones[arg]; 26 | } 27 | 28 | if(arg < 100) 29 | { 30 | //divided arg by 10 gives it the correct index position in the array of tens 31 | //example: 80 is at position 8 in the tens array 32 | //80/10 = 8. tens[8] = "eighty" 33 | return tens[arg/10] + word(arg%10); 34 | } 35 | 36 | // if the number is not an even hundred value i.e 100, 200, 300 37 | // then you have to add and to it according to the problem specs 38 | //getting the remainder when divided by 100 allows you to see 39 | //if the number needs an and 40 | // ex: 124 % 100 = 24, so the answer would be onehundredandtwentyfour 41 | withAnd = word(arg % 100); 42 | 43 | if(withAnd.length() > 0) 44 | withAnd = "and" + withAnd; 45 | 46 | return hundreds[arg/100] + withAnd; 47 | }//end word 48 | 49 | public int letterCt(int arg) 50 | { 51 | // System.out.println(word(arg)); 52 | // System.out.println(word(arg).length()); 53 | return word(arg).length(); 54 | } 55 | 56 | public static void main(String[] args) 57 | { 58 | int result = 0; 59 | 60 | for(int i = 1; i <= 1000; i++) 61 | { 62 | // System.out.println("numWord: " + i); 63 | result += new problem17().letterCt(i); 64 | } 65 | 66 | System.out.println("Result: " + result); 67 | }//end main 68 | }//end -------------------------------------------------------------------------------- /java/problem_20.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class problem20{ 3 | 4 | static ArrayList v=new ArrayList(); 5 | static void multiply(int x) 6 | { 7 | int carry = 0; 8 | int size = v.size(); 9 | for (int i = 0 ; i < size ; i++) 10 | { 11 | // Calculate res + prev carry 12 | int res = carry + v.get(i) * x; 13 | 14 | // updation at ith position 15 | v.set(i,res % 10); 16 | carry = res / 10; 17 | } 18 | while (carry != 0) 19 | { 20 | v.add(carry % 10); 21 | carry /= 10; 22 | } 23 | } 24 | 25 | // Returns sum of digits in n! 26 | static int findSumOfDigits(int n) 27 | { 28 | v.add(1); // adds 1 to the end 29 | 30 | // One by one multiply i to current vector 31 | // and update the vector. 32 | for (int i=1; i<=n; i++) 33 | multiply(i); 34 | 35 | // Find sum of digits in vector v[] 36 | int sum = 0; 37 | int size = v.size(); 38 | for (int i = 0 ; i < size ; i++) 39 | sum += v.get(i); 40 | return sum; 41 | } 42 | 43 | // Driver code 44 | public static void main(String[] args) 45 | { 46 | int n = 100; 47 | System.out.println(findSumOfDigits(n)); 48 | } 49 | } -------------------------------------------------------------------------------- /java/problem_25.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | 3 | class Problem25 4 | { 5 | public static void main(String[] args) 6 | { 7 | long begin = System.currentTimeMillis(); 8 | 9 | BigInteger a = BigInteger.valueOf(1); 10 | BigInteger b = BigInteger.valueOf(2); 11 | BigInteger c = BigInteger.valueOf(0); 12 | BigInteger MAX = new BigInteger("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); 13 | 14 | int i = 3; 15 | for (i = 3; b.compareTo(MAX) < 0; i++) 16 | { 17 | c = a.add(b); 18 | a = b; 19 | b = c; 20 | } 21 | System.out.println("index is : " + i); 22 | 23 | long end = System.currentTimeMillis(); 24 | System.out.println(end - begin + "ms"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /python/problem_1.py: -------------------------------------------------------------------------------- 1 | #If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 2 | 3 | #Find the sum of all the multiples of 3 or 5 below 1000. 4 | 5 | sm=0 6 | for i in range(1000): 7 | if i%3==0 or i%5==0: 8 | sm+=i 9 | print('Sum is ',sm) 10 | -------------------------------------------------------------------------------- /python/problem_10.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ Provides the sum of all the primes below two million""" 4 | 5 | def is_prime(num): 6 | """is_prime checks for the number "num" 7 | if the number "num" is prime it returns true 8 | if not prime returns false""" 9 | 10 | if (num<=1): 11 | return False 12 | for i in range(2,int (num**0.5)+1): 13 | if (num%i == 0): 14 | return False 15 | return True 16 | #initialize variable "sum" 17 | sum = 0 18 | #iterate the loop from 2 to 2 million 19 | for i in range(2,2000000): 20 | #check if num is prime 21 | if ( is_prime(i)): 22 | # If num is prime , Add num to variable sum 23 | sum = sum + i 24 | #print value of sum 25 | print(sum) 26 | -------------------------------------------------------------------------------- /python/problem_11.py: -------------------------------------------------------------------------------- 1 | ''' 2 | In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 3 | The product of these numbers is 26 × 63 × 78 × 14 = 1788696. 4 | 5 | What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid? 6 | ''' 7 | 8 | grid = [ 9 | [ 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8], 10 | [49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0], 11 | [81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65], 12 | [52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91], 13 | [22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80], 14 | [24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50], 15 | [32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70], 16 | [67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21], 17 | [24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72], 18 | [21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95], 19 | [78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92], 20 | [16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57], 21 | [86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58], 22 | [19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40], 23 | [ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66], 24 | [88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69], 25 | [ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36], 26 | [20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16], 27 | [20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54], 28 | [ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48], 29 | ] 30 | total = 4 31 | 32 | def prod(a,b,c,d,e): 33 | val = 1 34 | for i in range(e): 35 | val *= grid[b+i*d][a+i*c] 36 | return val 37 | 38 | final = -1 39 | width = len(grid[0]) 40 | height = len(grid) 41 | for i in range(height): 42 | for j in range(width): 43 | if i + total <= width: 44 | final = max(prod(i, j, 1, 0, total), final) 45 | if j + total <= height: 46 | final = max(prod(i, j, 0, 1, total), final) 47 | if i + total <= width and j + total <= height: 48 | final = max(prod(i, j, 1, 1, total), final) 49 | if i - total >= -1 and j + total <= height: 50 | final = max(prod(i, j, -1, 1, total), final) 51 | print(final) 52 | -------------------------------------------------------------------------------- /python/problem_12.py: -------------------------------------------------------------------------------- 1 | def sumOfNos(n): 2 | return sum([i for i in range(1, n+1)]) 3 | 4 | def factorCount(n): 5 | ''' 6 | To find the count of factors of the number using the prime factorization of the number 7 | ''' 8 | d = {} 9 | for i in primes: 10 | if n%i == 0: 11 | c = 0 12 | while n%i == 0: 13 | c+=1 14 | n = n//i 15 | d[i] = c 16 | if n <= i: 17 | break 18 | t = 1 19 | for i in d.values(): 20 | t *= (i+1) 21 | return t 22 | 23 | primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] 24 | i=1 25 | while True: 26 | if factorCount(sumOfNos(i)) >= 500: 27 | print(sumOfNos(i)) 28 | break 29 | i+=1 30 | -------------------------------------------------------------------------------- /python/problem_13.py: -------------------------------------------------------------------------------- 1 | #Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 2 | # 3 | #37107287533902102798797998220837590246510135740250 4 | #46376937677490009712648124896970078050417018260538 5 | #74324986199524741059474233309513058123726617309629 6 | #91942213363574161572522430563301811072406154908250 7 | #23067588207539346171171980310421047513778063246676 8 | #89261670696623633820136378418383684178734361726757 9 | #28112879812849979408065481931592621691275889832738 10 | #44274228917432520321923589422876796487670272189318 11 | #47451445736001306439091167216856844588711603153276 12 | #70386486105843025439939619828917593665686757934951 13 | #62176457141856560629502157223196586755079324193331 14 | #64906352462741904929101432445813822663347944758178 15 | #92575867718337217661963751590579239728245598838407 16 | #58203565325359399008402633568948830189458628227828 17 | #80181199384826282014278194139940567587151170094390 18 | #35398664372827112653829987240784473053190104293586 19 | #86515506006295864861532075273371959191420517255829 20 | #71693888707715466499115593487603532921714970056938 21 | #54370070576826684624621495650076471787294438377604 22 | #53282654108756828443191190634694037855217779295145 23 | #36123272525000296071075082563815656710885258350721 24 | #45876576172410976447339110607218265236877223636045 25 | #17423706905851860660448207621209813287860733969412 26 | #81142660418086830619328460811191061556940512689692 27 | #51934325451728388641918047049293215058642563049483 28 | #62467221648435076201727918039944693004732956340691 29 | #15732444386908125794514089057706229429197107928209 30 | #55037687525678773091862540744969844508330393682126 31 | #18336384825330154686196124348767681297534375946515 32 | #80386287592878490201521685554828717201219257766954 33 | #78182833757993103614740356856449095527097864797581 34 | #16726320100436897842553539920931837441497806860984 35 | #48403098129077791799088218795327364475675590848030 36 | #87086987551392711854517078544161852424320693150332 37 | #59959406895756536782107074926966537676326235447210 38 | #69793950679652694742597709739166693763042633987085 39 | #41052684708299085211399427365734116182760315001271 40 | #65378607361501080857009149939512557028198746004375 41 | #35829035317434717326932123578154982629742552737307 42 | #94953759765105305946966067683156574377167401875275 43 | #88902802571733229619176668713819931811048770190271 44 | #25267680276078003013678680992525463401061632866526 45 | #36270218540497705585629946580636237993140746255962 46 | #24074486908231174977792365466257246923322810917141 47 | #91430288197103288597806669760892938638285025333403 48 | #34413065578016127815921815005561868836468420090470 49 | #23053081172816430487623791969842487255036638784583 50 | #11487696932154902810424020138335124462181441773470 51 | #63783299490636259666498587618221225225512486764533 52 | #67720186971698544312419572409913959008952310058822 53 | #95548255300263520781532296796249481641953868218774 54 | #76085327132285723110424803456124867697064507995236 55 | #37774242535411291684276865538926205024910326572967 56 | #23701913275725675285653248258265463092207058596522 57 | #29798860272258331913126375147341994889534765745501 58 | #18495701454879288984856827726077713721403798879715 59 | #38298203783031473527721580348144513491373226651381 60 | #34829543829199918180278916522431027392251122869539 61 | #40957953066405232632538044100059654939159879593635 62 | #29746152185502371307642255121183693803580388584903 63 | #41698116222072977186158236678424689157993532961922 64 | #62467957194401269043877107275048102390895523597457 65 | #23189706772547915061505504953922979530901129967519 66 | #86188088225875314529584099251203829009407770775672 67 | #11306739708304724483816533873502340845647058077308 68 | #82959174767140363198008187129011875491310547126581 69 | #97623331044818386269515456334926366572897563400500 70 | #42846280183517070527831839425882145521227251250327 71 | #55121603546981200581762165212827652751691296897789 72 | #32238195734329339946437501907836945765883352399886 73 | #75506164965184775180738168837861091527357929701337 74 | #62177842752192623401942399639168044983993173312731 75 | #32924185707147349566916674687634660915035914677504 76 | #99518671430235219628894890102423325116913619626622 77 | #73267460800591547471830798392868535206946944540724 78 | #76841822524674417161514036427982273348055556214818 79 | #97142617910342598647204516893989422179826088076852 80 | #87783646182799346313767754307809363333018982642090 81 | #10848802521674670883215120185883543223812876952786 82 | #71329612474782464538636993009049310363619763878039 83 | #62184073572399794223406235393808339651327408011116 84 | #66627891981488087797941876876144230030984490851411 85 | #60661826293682836764744779239180335110989069790714 86 | #85786944089552990653640447425576083659976645795096 87 | #66024396409905389607120198219976047599490197230297 88 | #64913982680032973156037120041377903785566085089252 89 | #16730939319872750275468906903707539413042652315011 90 | #94809377245048795150954100921645863754710598436791 91 | #78639167021187492431995700641917969777599028300699 92 | #15368713711936614952811305876380278410754449733078 93 | #40789923115535562561142322423255033685442488917353 94 | #44889911501440648020369068063960672322193204149535 95 | #41503128880339536053299340368006977710650566631954 96 | #81234880673210146739058568557934581403627822703280 97 | #82616570773948327592232845941706525094512325230608 98 | #22918802058777319719839450180888072429661980811197 99 | #77158542502016545090413245809786882778948721859617 100 | #72107838435069186155435662884062257473692284509516 101 | #20849603980134001723930671666823555245252804609722 102 | #53503534226472524250874054075591789781264330331690 103 | # 104 | 105 | numlist = [ 106 | 37107287533902102798797998220837590246510135740250, 107 | 46376937677490009712648124896970078050417018260538, 108 | 74324986199524741059474233309513058123726617309629, 109 | 91942213363574161572522430563301811072406154908250, 110 | 23067588207539346171171980310421047513778063246676, 111 | 89261670696623633820136378418383684178734361726757, 112 | 28112879812849979408065481931592621691275889832738, 113 | 44274228917432520321923589422876796487670272189318, 114 | 47451445736001306439091167216856844588711603153276, 115 | 70386486105843025439939619828917593665686757934951, 116 | 62176457141856560629502157223196586755079324193331, 117 | 64906352462741904929101432445813822663347944758178, 118 | 92575867718337217661963751590579239728245598838407, 119 | 58203565325359399008402633568948830189458628227828, 120 | 80181199384826282014278194139940567587151170094390, 121 | 35398664372827112653829987240784473053190104293586, 122 | 86515506006295864861532075273371959191420517255829, 123 | 71693888707715466499115593487603532921714970056938, 124 | 54370070576826684624621495650076471787294438377604, 125 | 53282654108756828443191190634694037855217779295145, 126 | 36123272525000296071075082563815656710885258350721, 127 | 45876576172410976447339110607218265236877223636045, 128 | 17423706905851860660448207621209813287860733969412, 129 | 81142660418086830619328460811191061556940512689692, 130 | 51934325451728388641918047049293215058642563049483, 131 | 62467221648435076201727918039944693004732956340691, 132 | 15732444386908125794514089057706229429197107928209, 133 | 55037687525678773091862540744969844508330393682126, 134 | 18336384825330154686196124348767681297534375946515, 135 | 80386287592878490201521685554828717201219257766954, 136 | 78182833757993103614740356856449095527097864797581, 137 | 16726320100436897842553539920931837441497806860984, 138 | 48403098129077791799088218795327364475675590848030, 139 | 87086987551392711854517078544161852424320693150332, 140 | 59959406895756536782107074926966537676326235447210, 141 | 69793950679652694742597709739166693763042633987085, 142 | 41052684708299085211399427365734116182760315001271, 143 | 65378607361501080857009149939512557028198746004375, 144 | 35829035317434717326932123578154982629742552737307, 145 | 94953759765105305946966067683156574377167401875275, 146 | 88902802571733229619176668713819931811048770190271, 147 | 25267680276078003013678680992525463401061632866526, 148 | 36270218540497705585629946580636237993140746255962, 149 | 24074486908231174977792365466257246923322810917141, 150 | 91430288197103288597806669760892938638285025333403, 151 | 34413065578016127815921815005561868836468420090470, 152 | 23053081172816430487623791969842487255036638784583, 153 | 11487696932154902810424020138335124462181441773470, 154 | 63783299490636259666498587618221225225512486764533, 155 | 67720186971698544312419572409913959008952310058822, 156 | 95548255300263520781532296796249481641953868218774, 157 | 76085327132285723110424803456124867697064507995236, 158 | 37774242535411291684276865538926205024910326572967, 159 | 23701913275725675285653248258265463092207058596522, 160 | 29798860272258331913126375147341994889534765745501, 161 | 18495701454879288984856827726077713721403798879715, 162 | 38298203783031473527721580348144513491373226651381, 163 | 34829543829199918180278916522431027392251122869539, 164 | 40957953066405232632538044100059654939159879593635, 165 | 29746152185502371307642255121183693803580388584903, 166 | 41698116222072977186158236678424689157993532961922, 167 | 62467957194401269043877107275048102390895523597457, 168 | 23189706772547915061505504953922979530901129967519, 169 | 86188088225875314529584099251203829009407770775672, 170 | 11306739708304724483816533873502340845647058077308, 171 | 82959174767140363198008187129011875491310547126581, 172 | 97623331044818386269515456334926366572897563400500, 173 | 42846280183517070527831839425882145521227251250327, 174 | 55121603546981200581762165212827652751691296897789, 175 | 32238195734329339946437501907836945765883352399886, 176 | 75506164965184775180738168837861091527357929701337, 177 | 62177842752192623401942399639168044983993173312731, 178 | 32924185707147349566916674687634660915035914677504, 179 | 99518671430235219628894890102423325116913619626622, 180 | 73267460800591547471830798392868535206946944540724, 181 | 76841822524674417161514036427982273348055556214818, 182 | 97142617910342598647204516893989422179826088076852, 183 | 87783646182799346313767754307809363333018982642090, 184 | 10848802521674670883215120185883543223812876952786, 185 | 71329612474782464538636993009049310363619763878039, 186 | 62184073572399794223406235393808339651327408011116, 187 | 66627891981488087797941876876144230030984490851411, 188 | 60661826293682836764744779239180335110989069790714, 189 | 85786944089552990653640447425576083659976645795096, 190 | 66024396409905389607120198219976047599490197230297, 191 | 64913982680032973156037120041377903785566085089252, 192 | 16730939319872750275468906903707539413042652315011, 193 | 94809377245048795150954100921645863754710598436791, 194 | 78639167021187492431995700641917969777599028300699, 195 | 15368713711936614952811305876380278410754449733078, 196 | 40789923115535562561142322423255033685442488917353, 197 | 44889911501440648020369068063960672322193204149535, 198 | 41503128880339536053299340368006977710650566631954, 199 | 81234880673210146739058568557934581403627822703280, 200 | 82616570773948327592232845941706525094512325230608, 201 | 22918802058777319719839450180888072429661980811197, 202 | 77158542502016545090413245809786882778948721859617, 203 | 72107838435069186155435662884062257473692284509516, 204 | 20849603980134001723930671666823555245252804609722, 205 | 53503534226472524250874054075591789781264330331690, 206 | ] 207 | 208 | 209 | print('First 10 digits of sum is ',str(sum(numlist))[:10]) 210 | -------------------------------------------------------------------------------- /python/problem_14.py: -------------------------------------------------------------------------------- 1 | num=x=13 2 | i=0 3 | Dict={} 4 | lst=[] 5 | # n/2 even 6 | # 3n+1 odd 7 | 8 | while i<1000000: 9 | c=0 10 | x=num 11 | while x!=1: 12 | 13 | if(x%2 == 0): 14 | x/=2 15 | # print('inside if',num) 16 | else: 17 | x=3*x+1 18 | # print('inside else',num) 19 | c+=1 20 | 21 | # print(c,) 22 | Dict[c]=num 23 | num+=1 24 | i+=1 25 | # print(i) 26 | 27 | lst=list(Dict.keys()) 28 | print(max(lst)) 29 | -------------------------------------------------------------------------------- /python/problem_15.py: -------------------------------------------------------------------------------- 1 | moves=2 2 | row=int(input('Number of Rows: ')) 3 | col=int(input('Number of Cols: ')) 4 | 5 | print('Finding combination of total moves required...') 6 | 7 | cells=row*col 8 | 9 | def factors(x): 10 | f=1 11 | for i in range(1,int(x)+1): 12 | f*=i 13 | return f 14 | comb=factors(cells)/(2*factors(cells-moves)) 15 | print(round(comb)) -------------------------------------------------------------------------------- /python/problem_16.py: -------------------------------------------------------------------------------- 1 | 2 | # To find the sum of digits of the number 2^1000 3 | 4 | #splitting each digit and mapping it to an integer, and finally passing it to a sum function 5 | s = sum(map(int, str(2**1000))) 6 | print(s) 7 | 8 | -------------------------------------------------------------------------------- /python/problem_17.py: -------------------------------------------------------------------------------- 1 | def number_to_english(n: int) -> str: 2 | 3 | ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", 4 | "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] 5 | tens = [None, None, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] 6 | if 0 <= n < 20: 7 | return ones[n] 8 | elif 20 <= n <= 90 and n % 10 == 0: 9 | return tens[n // 10] 10 | elif 20 < n < 100: 11 | return tens[n // 10] + "-" + ones[n % 10] 12 | elif 100 <= n <= 900 and n % 100 == 0: 13 | return ones[n // 100] + " hundred" 14 | elif 100 < n < 1000: 15 | return ones[n // 100] + " hundred and " + number_to_english(n % 100) 16 | elif 1000 < n < 10000: 17 | pass 18 | elif n == 1000: 19 | return "one thousand" 20 | else: 21 | raise ValueError("unexpected input") 22 | 23 | 24 | def solve(): 25 | target = 1000 26 | answer = 0 27 | for i in range(target): 28 | words = number_to_english(i + 1).replace(" ", "").replace("-", "") 29 | answer += len(words) 30 | return answer 31 | 32 | print(solve()) 33 | #expected_answer = 21124 34 | -------------------------------------------------------------------------------- /python/problem_18.py: -------------------------------------------------------------------------------- 1 | # Sweta Snigdha Sahoo 2 | # author-swetasahoo29 3 | # question 4 | 5 | # By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 6 | # 3 7 | # 7 4 8 | # 2 4 6 9 | # 8 5 9 3 10 | # That is, 3 + 7 + 4 + 9 = 23. 11 | # Find the maximum total from top to bottom of the triangle below: 12 | # 75 13 | # 95 64 14 | # 17 47 82 15 | # 18 35 87 10 16 | # 20 04 82 47 65 17 | # 19 01 23 75 03 34 18 | # 88 02 77 73 07 63 67 19 | # 99 65 04 28 06 16 70 92 20 | # 41 41 26 56 83 40 80 70 33 21 | # 41 48 72 33 47 32 37 16 94 29 22 | # 53 71 44 65 25 43 91 52 97 51 14 23 | # 70 11 33 28 77 73 17 78 39 68 17 57 24 | # 91 71 52 38 17 14 91 43 58 50 27 29 48 25 | # 63 66 04 68 89 53 67 30 73 16 69 87 40 31 26 | # 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 27 | 28 | import time 29 | 30 | #time at the start of execution 31 | start = time.time() 32 | 33 | #numbers copied as string 34 | n = '''75 35 | 95 64 36 | 17 47 82 37 | 18 35 87 10 38 | 20 04 82 47 65 39 | 19 01 23 75 03 34 40 | 88 02 77 73 07 63 67 41 | 99 65 04 28 06 16 70 92 42 | 41 41 26 56 83 40 80 70 33 43 | 41 48 72 33 47 32 37 16 94 29 44 | 53 71 44 65 25 43 91 52 97 51 14 45 | 70 11 33 28 77 73 17 78 39 68 17 57 46 | 91 71 52 38 17 14 91 43 58 50 27 29 48 47 | 63 66 04 68 89 53 67 30 73 16 69 87 40 31 48 | 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23''' 49 | 50 | #splitting the number into a list 51 | n = n.strip().split('\n') 52 | 53 | #converting each and every list of strings to int 54 | for i in range(1,len(n)): 55 | n[i] = n[i].strip().split(' ') 56 | n[i] = [int(x) for x in n[i]] 57 | 58 | #adding the first number bcz we could not do the above 59 | #operation as this one one number 60 | n[0] = [75] 61 | 62 | #counter for counting number of iterations 63 | counter = 0 64 | 65 | #for loop for bottom-up approach 66 | for i in range(len(n)-2,-1,-1): 67 | for j in range(len(n[i])): 68 | n[i][j] = n[i][j] + max(n[i+1][j], n[i+1][j+1]) 69 | counter += 1 70 | n.pop() 71 | 72 | #printing the output 73 | print ('Found {} in {} iterations'.format(n[0][0],counter)) 74 | 75 | #time at the end of execution 76 | end = time.time() 77 | 78 | #printing the total time 79 | print (end-start) 80 | -------------------------------------------------------------------------------- /python/problem_19.py: -------------------------------------------------------------------------------- 1 | import time,calendar 2 | 3 | 4 | #time at the start of execution 5 | start = time.time() 6 | 7 | #changing the preference from monday to sunday 8 | calendar.setfirstweekday(6) 9 | 10 | 11 | def sundays(year): 12 | """This function returns 13 | The number of sundays in a year 14 | on the first of the month""" 15 | counter = 0 16 | for month in range(1,13): 17 | cal = calendar.monthcalendar(year,month) 18 | if cal[0][0]: 19 | counter += 1 20 | return counter 21 | 22 | #let the total number of sundays be 0 23 | total = 0 24 | 25 | #counting the sundays that fell on 26 | #first of every month for each year 27 | for i in range(1901,2001): 28 | total += sundays(i) 29 | 30 | #time at the end of execution 31 | end = time.time() 32 | 33 | #printing the output along with exection time 34 | print("Found {} in {} seconds".format(total,end-start)) -------------------------------------------------------------------------------- /python/problem_2.py: -------------------------------------------------------------------------------- 1 | #Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 2 | # 3 | #1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 4 | # 5 | #By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 6 | 7 | a,b=1,2 8 | sm=0 9 | i=0 10 | while True: 11 | if a%2==0: 12 | sm+=a 13 | elif a>=4000000: 14 | break 15 | if b%2==0: 16 | sm+=b 17 | elif b>=4000000: 18 | break 19 | a+=b 20 | b+=a 21 | print('Sum is ',sm) 22 | -------------------------------------------------------------------------------- /python/problem_20.py: -------------------------------------------------------------------------------- 1 | 2 | # Factorial sum 3 | # n! means n × (n − 1) × ... × 3 × 2 × 1 4 | 5 | # For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, 6 | # and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. 7 | 8 | # Find the sum of the digits in the number 100! 9 | 10 | import math 11 | n = 100 12 | print(sum(map(int, str(math.factorial(n))))) 13 | -------------------------------------------------------------------------------- /python/problem_21.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | L = 11000 3 | ds = [1]*L 4 | for i in range(2, int(sqrt(L))): 5 | for j in range(i+1, L//i): 6 | ds[i*j]+= i+j 7 | an = [] 8 | for i in range(2, L): 9 | if ds[i] < i and ds[ds[i]] == i: an+= [ds[i], i] 10 | N = 10000 11 | print("Amicable sum <",N,"=", sum(a for a in an if a n: 52 | abundentNums.append(n) 53 | 54 | sums = [0] * 28124 55 | for x in range(0, len(abundentNums)): 56 | for y in range(x, len(abundentNums)): 57 | sumOf2AbundantNums = abundentNums[x] + abundentNums[y] 58 | if (sumOf2AbundantNums <= 28123): 59 | if (sums[sumOf2AbundantNums] == 0): 60 | sums[sumOf2AbundantNums] = sumOf2AbundantNums 61 | 62 | ans = 0 63 | for i in range(1, len(sums)): 64 | if sums[i] == 0: 65 | ans += i 66 | 67 | print(ans) 68 | 69 | -------------------------------------------------------------------------------- /python/problem_24.py: -------------------------------------------------------------------------------- 1 | #To get the millionth permutation of 0,1,2,3,4,5,6,7,8,9 2 | 3 | import itertools 4 | array_nums = list(range(10)) #we get a list of all the digits from 0 to 9 5 | num = itertools.islice(itertools.permutations(array_nums),999999,None) #itertools.islice is a 4 input method, check geeks for geeks for more 6 | print("".join(str(x) for x in next(num))) 7 | -------------------------------------------------------------------------------- /python/problem_25.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | a,b = 1,0 4 | for i in itertools.count(): 5 | if len(str(b)) == 1000: 6 | print(i) 7 | break 8 | a, b = b, a + b 9 | -------------------------------------------------------------------------------- /python/problem_26.py: -------------------------------------------------------------------------------- 1 | # Sweta Snigdha Sahoo 2 | # author-swetasahoo29 3 | # question 4 | # A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 5 | 6 | # 1/2 = 0.5 7 | # 1/3 = 0.(3) 8 | # 1/4 = 0.25 9 | # 1/5 = 0.2 10 | # 1/6 = 0.1(6) 11 | # 1/7 = 0.(142857) 12 | # 1/8 = 0.125 13 | # 1/9 = 0.(1) 14 | # 1/10 = 0.1 15 | # Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. 16 | 17 | # Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. 18 | 19 | def recurring_cycle(n, d): 20 | 21 | for x in range(1, d): 22 | if 1 == 10**x % d: 23 | return x 24 | return 0 25 | 26 | longest = max(recurring_cycle(1, i) for i in range(2,1001)) 27 | print([i for i in range(2,1001) if recurring_cycle(1, i) == longest][0]) 28 | -------------------------------------------------------------------------------- /python/problem_27.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def isPrime(n): 5 | if n <= 1: 6 | return False 7 | if n == 2: 8 | return True 9 | for i in range(3, math.floor(math.sqrt(n)) + 1): 10 | if n % i == 0: 11 | return False 12 | return True 13 | 14 | 15 | primes = [] 16 | 17 | for i in range(1000): 18 | if isPrime(i): 19 | primes.append(i) 20 | 21 | longest = 0 22 | largestA = 0 23 | largestB = 0 24 | 25 | for a in range(-1000, 1000): 26 | for b in primes: 27 | n = 0 28 | term = n ** 2 + a * n + b 29 | 30 | while isPrime(term): 31 | term = n ** 2 + a * n + b 32 | n += 1 33 | 34 | if n > longest: 35 | longest = n 36 | largestA = a 37 | largestB = b 38 | 39 | print(largestA * largestB) 40 | -------------------------------------------------------------------------------- /python/problem_3.py: -------------------------------------------------------------------------------- 1 | #The prime factors of 13195 are 5, 7, 13 and 29. 2 | # 3 | #What is the largest prime factor of the number 600851475143 ? 4 | 5 | num=600851475143 6 | pf = 1 7 | i = 2 8 | while i <= num / i: 9 | if num % i == 0: 10 | pf = i 11 | num /= i 12 | else: 13 | i += 1 14 | 15 | if pf < num: 16 | pf = num 17 | print('Largest Prime: ',int(pf)) 18 | -------------------------------------------------------------------------------- /python/problem_30.py: -------------------------------------------------------------------------------- 1 | def solution(): 2 | return sum(i for i in range(2, 1000000) if i == sum(int(_)**5 for _ in str(i))) 3 | 4 | print(solution()) 5 | -------------------------------------------------------------------------------- /python/problem_301.py: -------------------------------------------------------------------------------- 1 | def compute(): 2 | a = 0 3 | b = 1 4 | for i in range(32): 5 | a, b = b, a + b 6 | return str(a) 7 | 8 | 9 | if __name__ == "__main__": 10 | print(compute()) 11 | -------------------------------------------------------------------------------- /python/problem_31.py: -------------------------------------------------------------------------------- 1 | # https://projecteuler.net/problem=31 2 | 3 | target = 200 4 | coins = [1, 2, 5, 10, 20, 50, 100, 200] 5 | dp = [0 for i in range(target + 1)] 6 | dp[0] = 1 7 | 8 | for i in range(len(coins)): 9 | for j in range(coins[i], target + 1): 10 | dp[j] += dp[j - coins[i]] 11 | 12 | print(dp[-1]) 13 | 14 | -------------------------------------------------------------------------------- /python/problem_34.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Oct 1 22:33:50 2020 4 | 5 | @author: HP 6 | """ 7 | 8 | # Problem 34 9 | def factorial(num): 10 | product = num 11 | if product==0: 12 | return 1 13 | elif product==1: 14 | return 1 15 | else: 16 | for i in range(2, num): 17 | product *= i 18 | return product 19 | 20 | def check_sum(number): 21 | list_digits = list(str(number)) 22 | check_sum = 0 23 | for digit in list_digits: 24 | check_sum += factorial(int(digit)) 25 | if check_sum == number: 26 | return True 27 | 28 | def find_final_sum(): 29 | final_list = [] 30 | final_sum = 0 31 | counter = 3 32 | while counter < 50000: 33 | if check_sum(counter): 34 | final_list.append(counter) 35 | counter += 1 36 | else: 37 | counter += 1 38 | 39 | for j in final_list: 40 | final_sum += j 41 | print(final_sum) 42 | 43 | find_final_sum() 44 | 45 | -------------------------------------------------------------------------------- /python/problem_37.py: -------------------------------------------------------------------------------- 1 | def isprime(n): 2 | 3 | # make sure n is a positive integer 4 | n = abs(int(n)) 5 | # 0 and 1 are not primes 6 | if n < 2: 7 | return False 8 | # 2 is the only even prime number 9 | if n == 2: 10 | return True 11 | # all other even numbers are not primes 12 | if not n & 1: 13 | return False 14 | # range starts with 3 and only needs to go up the squareroot of n 15 | # for all odd numbers 16 | for x in range(3, int(n ** 0.5) + 1, 2): 17 | if n % x == 0: 18 | return False 19 | return True 20 | 21 | def peel_left(i): 22 | 23 | x = list(str(i)) 24 | for n in range(1, len(x)): 25 | y = x[n:] 26 | s = int(''.join(y)) 27 | if isprime(s): 28 | continue 29 | else: 30 | return "False" 31 | return "True" 32 | 33 | def peel_right(i): 34 | 35 | x = list(str(i)) 36 | for n in range(1, len(x)): 37 | y = x[:(-n)] 38 | s = int(''.join(y)) 39 | if isprime(s): 40 | continue 41 | else: 42 | return "False" 43 | return "True" 44 | 45 | def main(): 46 | 47 | L = [] 48 | for i in range(10, 1000000): 49 | if isprime(i): 50 | if (peel_left(i) == "True") and (peel_right(i) == "True"): 51 | L.append(i) 52 | else: 53 | continue 54 | else: 55 | continue 56 | print "L = ", L 57 | print "Answer = ", sum(L) 58 | 59 | if __name__ == '__main__': 60 | main() 61 | -------------------------------------------------------------------------------- /python/problem_4.py: -------------------------------------------------------------------------------- 1 | #A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. 2 | # 3 | #Find the largest palindrome made from the product of two 3-digit numbers. 4 | 5 | big=0 6 | x,y=0,0 7 | i=999 8 | while i>900: 9 | j=999 10 | while j>900: 11 | m=i*j 12 | if m>big: 13 | if str(m)==str(m)[ : :-1]: 14 | big=m 15 | x,y=i,j 16 | print(str(x) +' X '+ str(y) +' = '+str(big)) 17 | exit() 18 | j-=1 19 | i-=1 20 | -------------------------------------------------------------------------------- /python/problem_45.py: -------------------------------------------------------------------------------- 1 | from itertools import count 2 | 3 | def polygonal_numbers(sides): 4 | result = 0 5 | for n in count(): 6 | yield result 7 | result += (sides - 2) * n + 1 8 | 9 | tt, pp, hh = polygonal_numbers(3), polygonal_numbers(5), polygonal_numbers(6) 10 | t = p = 0 11 | for h in hh: 12 | while p < h: p = next(pp) 13 | while t < h: t = next(tt) 14 | if t == p == h > 40755: 15 | print(h) 16 | break -------------------------------------------------------------------------------- /python/problem_5.py: -------------------------------------------------------------------------------- 1 | # 2 | #2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 3 | # 4 | #What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 5 | import math 6 | 7 | res = 2520 8 | for i in range(11, 21): 9 | res *= int(i / math.gcd(i, res)) 10 | print('LCM is ',res) 11 | -------------------------------------------------------------------------------- /python/problem_50.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | The prime 41, can be written as the sum of six consecutive primes: 4 | 5 | 41 = 2 + 3 + 5 + 7 + 11 + 13 6 | This is the longest sum of consecutive primes that adds to a prime below one-hundred. 7 | 8 | The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. 9 | 10 | Which prime, below one-million, can be written as the sum of the most consecutive primes? 11 | 12 | ''' 13 | 14 | def primes(n): 15 | """ Returns a list of primes < n """ 16 | sieve = [True] * n 17 | for i in range(3,int(n**0.5)+1,2): 18 | if sieve[i]: 19 | sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) 20 | return [2] + [i for i in range(3,n,2) if sieve[i]] 21 | 22 | # Generating all prime numbers between 1 to 1million 23 | primes = primes(1000000) 24 | 25 | # length of the consecutive prime sum 26 | length = 0 27 | 28 | # value of the consecutive prime sum 29 | largest = 0 30 | 31 | # max value of the j variable(second for loop) 32 | lastj = len(primes) 33 | 34 | # two for loops 35 | for i in range(lastj): 36 | for j in range(i+length, lastj): 37 | sol = sum(primes[i:j]) 38 | if sol < 1000000: 39 | if sol in primes: 40 | length = j-i 41 | largest = sol 42 | else: 43 | lastj = j+1 44 | break 45 | 46 | # printing the requried solution 47 | print (largest) 48 | -------------------------------------------------------------------------------- /python/problem_51.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | By replacing the 1st digit of the 2-digit number *3, 4 | it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. 5 | 6 | By replacing the 3rd and 4th digits of 56**3 with the same digit, 7 | this 5-digit number is the first example having seven primes among the ten generated numbers, 8 | yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, 9 | being the first member of this family, is the smallest prime with this property. 10 | 11 | Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) 12 | with the same digit, is part of an eight prime value family. 13 | 14 | ''' 15 | 16 | from collections import Counter 17 | 18 | def primes(n): 19 | """ Returns a list of primes < n """ 20 | sieve = [True] * n 21 | for i in range(3,int(n**0.5)+1,2): 22 | if sieve[i]: 23 | sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) 24 | return [2] + [i for i in range(3,n,2) if sieve[i]] 25 | 26 | # Generating all prime numbers between 1 to 1million 27 | primes = primes(1000000) 28 | 29 | # primes with 3 replacable places 30 | primes = [x for x in primes if len(str(x)) - len(set(str(x))) >= 3] 31 | 32 | 33 | def pdr(s): 34 | """take a number and return a list with families 35 | for example if the input number is 566003 then 36 | the result will be 37 | [[566003,566113,566223,566333,566443,566553,566663,566773,566883,566993], 38 | [500003,511003,522003,533003,544003,555003,566003,577003,588003,599003]]""" 39 | s = str(s) 40 | sol = [] 41 | for duplicate in (Counter(s) - Counter(set(s))): 42 | a = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 43 | temp = [int(s.replace(duplicate, x)) for x in a] 44 | sol.append(temp) 45 | return sol 46 | 47 | # list to store all the checked numbers, so not to repeat 48 | checked = [] 49 | 50 | 51 | def check(l): 52 | """take a list and remove all the values which are 53 | not prime numbers, finally return a list with only 54 | prime numbers""" 55 | for i in l: 56 | checked.append(i) 57 | if i not in primes: 58 | l.remove(i) 59 | return l 60 | 61 | # condition for while loop 62 | flag = True 63 | 64 | # while loop iterator 65 | i = 0 66 | 67 | # while loop 68 | while flag: 69 | # check if we have not check the number before 70 | if primes[i] not in checked: 71 | # find the family of the given number 72 | replacements = pdr(primes[i]) 73 | for j in replacements: 74 | if len(check(j)) == 8: 75 | print (j[0]) 76 | flag = False 77 | break 78 | i += 1 79 | -------------------------------------------------------------------------------- /python/problem_52.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | It can be seen that the number, 125874, and its double, 251748, 4 | contain exactly the same digits, but in a different order. 5 | 6 | Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. 7 | 8 | ''' 9 | 10 | # while loop iterator 11 | i = 1 12 | 13 | # while loop 14 | while True: 15 | if set(str(i)) == set(str(6*i)): 16 | if set(str(i)) == set(str(5*i)): 17 | if set(str(i)) == set(str(4*i)): 18 | if set(str(i)) == set(str(3*i)): 19 | if set(str(i)) == set(str(2*i)): 20 | print (i) 21 | break 22 | i += 1 23 | 24 | -------------------------------------------------------------------------------- /python/problem_53.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Project Euler Problem 53 3 | visit for more details - https://projecteuler.net/problem=53 4 | 5 | Ans- 4075 6 | ''' 7 | import math 8 | 9 | MAX = 1000000 10 | count = 0 11 | 12 | for n in range(1,101): 13 | for r in range(1,n+1): 14 | 15 | C = math.factorial(n)/ ( math.factorial(r)*math.factorial(n-r) ) 16 | 17 | if C > MAX: 18 | count += 1 19 | print(count) 20 | -------------------------------------------------------------------------------- /python/problem_55.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | 4 | def is_palindrome(number): 5 | """returns True for a palindrome, False otherwise.""" 6 | to_str = str(number) 7 | return to_str == to_str[::-1] 8 | 9 | 10 | def add_reverses(number, count): 11 | """makes a list of reverses for a number at a maximum iteration of count, breaks if palindrome found below count.""" 12 | reverses = [number] 13 | for _ in range(count): 14 | last_number = reverses[-1] 15 | new_number = int(str(reverses[-1])[::-1]) 16 | if is_palindrome(last_number + new_number): 17 | reverses.append(last_number + new_number) 18 | return reverses 19 | else: 20 | reverses.append(last_number + new_number) 21 | return reverses 22 | 23 | 24 | def count_lychrel_range(number_range, count): 25 | """returns Lychrel numbers within number_range, assumes count is the maximum iterations for a number.""" 26 | total = 0 27 | numbers_reverses = {} 28 | for number in range(number_range): 29 | numbers_reverses[number] = add_reverses(number, count) 30 | for number, rev_sequence in numbers_reverses.items(): 31 | if len(rev_sequence) >= count: 32 | total += 1 33 | return total 34 | 35 | 36 | if __name__ == '__main__': 37 | start_time = time() 38 | n = 10000 39 | print(f'Total Lychrel numbers below {n}: {count_lychrel_range(n, 50)}') 40 | print(f'Time: {time() - start_time} seconds.') 41 | -------------------------------------------------------------------------------- /python/problem_57.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | # time at the start of program execution 4 | start = time.time() 5 | 6 | # p 7 | p = 1 8 | 9 | # q 10 | q = 1 11 | 12 | # counter 13 | counter = 0 14 | 15 | # 1000 iterations 16 | for i in range(1000): 17 | a1 = p + 2*q 18 | b1 = p + q 19 | if len(str(a1)) > len(str(b1)): 20 | counter += 1 21 | p = a1 22 | q = b1 23 | 24 | # printing the counter 25 | print (counter) 26 | 27 | # time at the end of program execution 28 | end = time.time() 29 | 30 | # total time of execution 31 | print (end - start) 32 | -------------------------------------------------------------------------------- /python/problem_58.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | start = time.time() 4 | 5 | 6 | def is_prime(n): 7 | 8 | for i in range(3, int(n**0.5)+1, 2): 9 | if n % i == 0: 10 | return False 11 | return True 12 | 13 | # iterator for the numbers 14 | i = 0 15 | 16 | # gap for every four iterations 17 | gap = 1 18 | 19 | # ratio of lenght of primes to length of diagonals 20 | ratio = 1 21 | 22 | # prime numbers on the diagonal 23 | primes = [] 24 | 25 | # all the numbers, including 1 26 | all_numbers = [1] 27 | 28 | # While loop, to loop until we reach the last number 29 | while ratio > 0.1: 30 | for j in range(4): 31 | # generating the value of n for 2n+1 32 | i += gap 33 | # generating the odd number 34 | present_number = 2*i + 1 35 | # appending the number to all_numbers 36 | all_numbers.append(present_number) 37 | # appending to prime if the number is prime 38 | if is_prime(present_number): 39 | primes.append(2*i + 1) 40 | # changing the value of ratio at the end of loop 41 | ratio = float(len(primes))/len(all_numbers) 42 | # increasing the gap after every 4 numbers 43 | gap += 1 44 | 45 | # printing the side length 46 | print ((2*i+1)**0.5) 47 | 48 | # time at the end of the program execution 49 | end = time.time() 50 | 51 | # printing the total time of execution 52 | print (end - start) 53 | -------------------------------------------------------------------------------- /python/problem_6.py: -------------------------------------------------------------------------------- 1 | #The sum of the squares of the first ten natural numbers is, 2 | # 3 | #12 + 22 + ... + 102 = 385 4 | #The square of the sum of the first ten natural numbers is, 5 | # 6 | #(1 + 2 + ... + 10)2 = 552 = 3025 7 | #Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. 8 | # 9 | #Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. 10 | 11 | n=100 12 | 13 | si=n*(n+1)*(2*n+1)/6 14 | sa=(n*(n+1)/2)**2 15 | 16 | print('Difference is ',int(sa-si)) 17 | 18 | -------------------------------------------------------------------------------- /python/problem_7.py: -------------------------------------------------------------------------------- 1 | #By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 2 | # 3 | #What is the 10 001st prime number? 4 | import math 5 | def is_prime(x): 6 | if x <= 1: 7 | return False 8 | elif x <= 3: 9 | return True 10 | elif x % 2 == 0: 11 | return False 12 | else: 13 | for i in range(3, int(math.sqrt(x)) + 1, 2): 14 | if x % i == 0: 15 | return False 16 | return True 17 | 18 | 19 | n=10001 20 | pr=1 21 | num=3 22 | while not pr==n: 23 | if is_prime(num): 24 | pr+=1 25 | num+=2 26 | print('Number is',(num-2)) 27 | -------------------------------------------------------------------------------- /python/problem_75.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | import math 3 | 4 | primes=set([2,3,5,7,11,13,17,19,23]) 5 | def isPrime(n): 6 | n=abs(n) 7 | if n in primes: 8 | return True 9 | if n<2: 10 | return False 11 | if n==2: 12 | return True 13 | if n%2==0: 14 | return False 15 | for x in range(3, int(n**0.5)+1, 2): 16 | if n % x == 0: 17 | return False 18 | primes.add(n) 19 | return True 20 | 21 | def aFactors(n): 22 | if isPrime(n): 23 | return [1,n] 24 | return set(reduce(list.__add__,([i,n//i] for i in range(1,int(math.sqrt(n))+1) if n%i==0))) 25 | 26 | 27 | count=0 28 | number=12 29 | while number<=1500000: 30 | p=number/2 31 | f=aFactors(p) 32 | triangles=[] 33 | pairs=[(i, int(p/i)) for i in f] 34 | add=triangles.append 35 | for i in pairs: 36 | mList=aFactors(i[0]) 37 | pairsOfmc=[(k,int(i[0]/k)) for k in mList] 38 | for j in pairsOfmc: 39 | add((2*i[1]*i[0]-i[1]*i[1]*j[0],2*i[0]*i[1]-2*i[0]*j[1],2*i[0]*j[1]+i[1]*i[1]*j[0]-2*i[0]*i[1])) 40 | r=0 41 | while r