Skip to main content

All Questions

Filter by
Sorted by
Tagged with
59 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar
33 votes
7 answers
6k views

Project Euler #7 10001st prime

I decided to start working on the Euler project exercises. I'm doing fine for now but the seventh problem is running quite slow and I can't think of anything to make it work faster. It takes around ...
Denis's user avatar
  • 8,508
32 votes
8 answers
5k views

Project Euler Problem 45

As a self-teaching Python beginner for almost 4 months, I have mostly been doing online challenges including Project Euler problems. Problem 45 asks: Triangle, pentagonal, and hexagonal numbers ...
ifsoMarcus's user avatar
32 votes
6 answers
3k views

Aliens at the train

I solved this problem on SPOJ: The aliens have arrived to Earth and everything is in harmony, the two races can live together. However, one specific Female Alien does not want to see humans on ...
Nitin Labhishetty's user avatar
30 votes
3 answers
27k views

Perm-Missing-Elem: 100% functional score, but only 60% performance

I cannot figure out why I didn't get 100% success from the Codility's Perm-Missing-Element test even I solve with \$O(n)\$. I will appreciate any advice/answers. You can see the problem, my code, and ...
quartaela's user avatar
  • 505
30 votes
6 answers
4k views

DailyProgrammer 284: Wandering Fingers

Description Software like Swype and SwiftKey lets smartphone users enter text by dragging their finger over the on-screen keyboard, rather than tapping on each letter. You'll be given ...
px06's user avatar
  • 403
26 votes
2 answers
6k views

Printing 1,000,000 numbers in 1 sec. in Python

Here's a fairly simple task from CSES Problem Set - Permutations 1070 that reads: A permutation of integers 1,2, …, n is called beautiful if there are no adjacent elements whose difference is 1. ...
Bor's user avatar
  • 507
25 votes
2 answers
2k views

Speed up solution to Project Euler problem 75

I wrote this code for Project Euler problem 75, which asks how many integers ≤ 1500000 exist, where the integer is a perimeter length that can be divided into three integer-length sides of a right ...
user2312505's user avatar
25 votes
5 answers
2k views

Count distinct primes, discarding palindromes, in under 2 seconds

Problem Statement Generate as many distinct primes P such that reverse (P) is also prime and is not equal to P. Output: Print per line one integer( ≤ 1015 ). Don't print more than 106 ...
Akash Rana's user avatar
24 votes
12 answers
4k views

Project Euler 8: Maximum product of 13 adjacent digits

Problem 8 on Project Euler which asks Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the product? This is my solution in Visual C#. ...
Paras's user avatar
  • 585
23 votes
5 answers
18k views

Finding the 10001st prime

I'm solving Project Euler problem 7, which says: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Here's the ...
Nicholas Hassan's user avatar
20 votes
5 answers
3k views

Print the N longest lines in a file

I have implemented the "Find the N longest lines in a file" problem from CodeEval quoted below. I got a full 100 score and 182ms execution time of their data set on the site so I consider the code ...
Emily L.'s user avatar
  • 16.6k
19 votes
6 answers
7k views

Determining if the kangaroos will land in the same position

Problem There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters ...
amallard's user avatar
  • 365
19 votes
2 answers
2k views

"ACM ICPC Team" challenge on Hackerrank... Easy?

This challenge took me awhile to figure out, and it's characterized as "easy," so I'm sorta wondering what I missed. Problem Statement You are given a list of N people who are attending ACM-...
Robert Harvey's user avatar
17 votes
4 answers
2k views

Duplicate like a weapon, arrays like heaven

Challenge: Find the duplicated entry. Specifications: Your program should accept as its first argument a path to a filename. Each line in this file is one test case. Each line begins ...
Legato's user avatar
  • 9,839
16 votes
6 answers
4k views

Sherlock and The Beast

This is my solution to this question from Hackerrank. Given N (<= 100000), find the largest N-digit number such that: The number has only 3 and 5 as its digits. Number of times 3 ...
sundar nataraj's user avatar
16 votes
4 answers
3k views

Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

A friend of mine recently started learning R, and she complained to me that R is very slow. She was working on Project Euler Problem 7, which asks for the value of the 10001st prime number. For ...
castle-bravo's user avatar
13 votes
5 answers
22k views

Smallest Multiple

I just recently learned about Project Euler and have started doing the problems on there. I cleared problem 1 and 2, had no idea how to do 3 and 4, and started to do 5. I've seen the post regarding ...
Sephallia's user avatar
  • 233
13 votes
3 answers
4k views

Codewars: Reduce strings to one character

I am trying to solve a puzzle in a performant way. This is the puzzle. Simply explained: you have a String and you must reduce it to one character with the given rules. This is the solution to the ...
Superluminal's user avatar
13 votes
3 answers
8k views

Find total number of phone numbers formed by the movement of Knight and Bishop on keypad

I recently took a test on HackerRank, and submitted my answer for the following question. I passed all the test cases but company told me that the answer is not efficient, and they are not moving ...
python's user avatar
  • 623
13 votes
4 answers
660 views

Finding the largest of a series of numbers being XORed

I'm working on solving a problem involving finding the largest of a series of numbers being XORed. This is the actual problem: Xorq has invented an encryption algorithm which uses bitwise XOR ...
Ryan McClure's user avatar
13 votes
3 answers
5k views

The Beauty and the Strings

This originally appeared in Facebook's hacker cup 2013. Solving it was fun, though I find myself looping through similar data quite often. Challenge: Print the maximum beauty of strings. ...
Legato's user avatar
  • 9,839
12 votes
3 answers
6k views

Project Euler Problem 12 - triangle number with 500 divisors

I've just done Problem 12 of Project Euler: What is the value of the first triangle number to have over five hundred divisors? The \$N\$'th triangle number is the sum of all natural numbers ...
Connor Cartwright's user avatar
12 votes
3 answers
16k views

Project Euler Problem 12 in Python

I'm trying to do Project Euler Problem 12, which reads as: 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 + ...
Ali's user avatar
  • 223
12 votes
2 answers
599 views

Cows with walkie-talkies (USACO Silver Prob. #3; December '16 )

The official problem description is here, which in my opinion is unclear and quite long. Below is my description. I recommend you still read the official description, it has details such as input and ...
noɥʇʎԀʎzɐɹƆ's user avatar
12 votes
2 answers
5k views

Generating 'Decent' numbers

While solving a problem from HackerRank, though it's an easy one, I wrote this big program. Not sure if recursion is applicable and hold good but to me it looked like it isn't. However when I run the ...
bluefoggy's user avatar
  • 235
12 votes
2 answers
5k views

Contest code for a maze problem

I would like to ask your comments on my contest code for the following problem: We're going to have a slightly more logical puzzle today. We're going to write a program that will find a path through ...
MykelXIII's user avatar
  • 223
11 votes
3 answers
6k views

Algorithm for competing cells of 0s and 1s

I'm working on a practice algorithm problem, stated as follows: There are eight houses represented as cells. Each day, the houses compete with adjacent ones. 1 represents an "active" house and 0 ...
LuxuryMode's user avatar
11 votes
4 answers
27k views

Find the binary gap of a number N

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of ...
Cormack's user avatar
  • 201
11 votes
3 answers
1k views

Optimizing "Herd Sums" problem using dynamic programming

I'm trying to solve a practice problem and all is well except for the last 2 inputs my solution is too slow. Just one loop is slowing it down I think. Herd Sums Execution Time Limit: 2 seconds ...
jantristanmilan's user avatar
11 votes
2 answers
172 views

What a crooked way to compute the next straight string (advent of code 11)

This year I have been participating in the Advent of Code series of challenges, and it just so happened that I'd be doing them in Javascript. Not my usual weapon of choice, but I do have some ...
John Dvorak's user avatar
  • 1,228
10 votes
9 answers
13k views

Project Euler #5 - Lowest Multiple of 1 through 20

I was trying to solve problem 5 of Project Euler, and this was I needed to do: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What ...
Alexander Ameye's user avatar
10 votes
2 answers
3k views

Project Euler #7 10001st prime in C++

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? How can I optimize this code? ...
coder's user avatar
  • 2,451
10 votes
3 answers
1k views

Solution to Project Euler Problem #37 in C++: Truncatable Primes

I was recently looking at some of my old Project Euler solutions, and saw that my implementation for Problem 37 could be improved drastically. The problem reads as follows: The number 3797 has an ...
Arnav Borborah's user avatar
10 votes
3 answers
537 views

How can I make my solution for Chef and Digits run faster?

Problem Statement Given \$n\$ digits and \$m\$ indices \$x\$ from \$1. \ldots,n\$, calculate the difference \$b_y = a_x - a_y\$ for all indices \$y \; (y < x)\$. Then, calculate \$B1\$, the ...
Akash Rana's user avatar
10 votes
2 answers
4k views

Finding the Mth to last Element

Challenge: Write a program which determines the Mth to last element in a list. Specifications: The first argument is a path to a file. The file contains series of space delimited characters ...
Legato's user avatar
  • 9,839
10 votes
1 answer
2k views

Google Kickstart Round A 2019 - Training

Problem: Training As the football coach at your local school, you have been tasked with picking a team of exactly P students to represent your school. There are N students for you to pick ...
Eric's user avatar
  • 261
10 votes
2 answers
624 views

The finest integers are palindromes

Challenge: Reverse the digits of a number and add it to the original until the number is a palindrome Specifications: Your program should accept as its first argument a path to a filename. ...
Legato's user avatar
  • 9,839
10 votes
3 answers
491 views

Selecting kids for a Christmas play with similar heights

I am doing this problem on SPOJ: My kid's kindergarten class is putting up a Christmas play. (I hope he gets the lead role.) The kids are all excited, but the teacher has a lot of work. She has ...
Suraj Jain's user avatar
10 votes
1 answer
305 views

Tweets per second, using linked list

This is a TalentBuddy Challenge Your task is to write a function that prints to the standard output (stdout) the highest number of tweets recorded between each second in the array and the past <...
BenVlodgi's user avatar
  • 4,278
9 votes
4 answers
2k views

Leetcode : First Missing Positive

I was trying out leetcode's first missing positive. As per the challenge's description: Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an ...
ccot's user avatar
  • 341
9 votes
3 answers
2k views

LeetCode 1108: Defanging an IP Address

I'm posting two solutions for LeetCode's "Defanging an IP Address". If you'd like to review, please do. Thank you! Problem Given a valid (IPv4) IP address, return a defanged version of that ...
Emma's user avatar
  • 3,542
9 votes
2 answers
11k views

Optimizing solution for Project Euler Problem #23 (non-abundant sums)

Project Euler Problem 23 asks: A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 ...
Mohammad Areeb Siddiqui's user avatar
9 votes
5 answers
718 views

Ten thousand and first prime checker takes a very long time

I am attempting problem 7 on Project Euler. I have come up with this solution which works fine for finding smaller nth prime numbers, but really lags when it comes to higher nth prime numbers. I am ...
user2405469's user avatar
9 votes
3 answers
626 views

CSES standard solution for range query problem giving TLE

Problem statement is from CSES This is a standard question where we are given a list of numbers and a number of queries. For each query, you have to give the sum of numbers in the given range. My ...
YetAnotherBot's user avatar
9 votes
3 answers
11k views

Project Euler #9 - Pythagorean triplets

This code works, and I'm trying to figure out if there's a better way to optimize it. As you can see, I've cut down drastically on time just by using numbers that work for ...
Jose Magana's user avatar
9 votes
1 answer
2k views

Codility MaxCounters peformance in Python capping out at 77% performance

My problem is quite similar to the question found here, except I am attempting to answer the question in Python. Given an array of N counters, all initialized to 0, and an array ...
optical_anathema's user avatar
9 votes
1 answer
620 views

Horridly Inefficient Project Euler #14 in Python - Collatz Sequences

I created this solution a while ago, and have been going back over my problems to try to make them all more efficient. I have been stuck on this one, and have tried a few different ideas. So far, my ...
Jose Magana's user avatar
9 votes
2 answers
4k views

Python 3 two-sum performance

I just submitted a Python solution to the 'Two Sum' problem on LeetCode. The Problem Given an array of integers, return indices of the two numbers such that they add up to a specific target. ...
PragmaticProgrammer's user avatar
9 votes
2 answers
275 views

Tail implementation using a dynamic queue library

As per K & R exercise 5-13 Write the program tail, which prints the last n lines of its input. By default, n is 10, say, but it can be changed by an optional argument, so that ...
jess's user avatar
  • 1,378

1
2 3 4 5
13