Can Sup Forums find all palindromes numbers (reads same forward and backwards) under 2 million and sum them?
Can Sup Forums find all palindromes numbers (reads same forward and backwards) under 2 million and sum them?
In what base you nignog
base 10
16
...
1000000 * 2000001
All palindrome numbers in base 1 below 2000000. Ez
isn't the trick just to take all the numbers up to 9999 and let the remaining digits be determined by those four?
CL-USER> (loop for x below 2000000 when (string= (write-to-string x) (reverse (write-to-string x))) sum x)
2045041040
Wait
I just realized that's a pc case
Wtf
That's a good start, but consider that not all palindromic numbers have max length, and zeroes are significant, ie. 2, 22, 202, 222, etc. are all palindromic
static void Main(string[] args)
{
List space = new List();
for (int i = 1; i < 2000000; i++)
{
space.Add(i);
}
Console.WriteLine(
space.Where(
o => (o.ToString() == string.Concat(o.ToString().Reverse())) && o > 10
).Sum()
);
}
2045040995
public static void main(String[] args) {
System.out.println(sumPalindromes(2000000));
}
public static int sumPalindromes(int max) {
int sum = 0;
for (int i = 0; i < max; i++) {
String originalNumber = Integer.toString(i);
String reversedNumber = "";
for (int j = originalNumber.length() - 1; j >= 0; j--) {
reversedNumber += originalNumber.charAt(j);
}
if (originalNumber.equals(reversedNumber)) {
sum += i;
}
}
return sum;
}
>2045041040
Remainder that having code maintainability is much more important than having fewer lines of code.
>not developing with TDD and having unit testing
>not adhering to strict OOP principles
>not making interfaces to easily extend functionality
>not having comments
>implying that is maintainable.
Can we consider 20 to be a palindrome by assuming a leading 0 (e.g. 20 == 020)?!?
sumpalindromesundertwomillion();
doEverythin()
>being autistic
Sorry you are being replaced by Rajesh
The sum of all palindromes under some power of 10 has a nice property. Sum of all the palindromes under 100, 10^3, 10^4, 10^5 ,10^6 makes the series
495, 49500, 495000, 49500000, 495000000.
This property should be obvious. The only hard part that requires any work is between 1x10^6-2x10^6. This is simply
Sum[b 10 ^6 + a 10^5 + c 10^4 + d 10^3 + c 10^2 + a 10 + b, {c, 0,
9}, {a, 0, 9}, {d, 0, 9}, {b, 1}] [\code]
Summing everything up gives 2045040995
That image makes me wanna get some chilly banana pudding now
So we've got:
2045041040
and
2045040995
Who is correct?
both
The difference is 45 (sum of numbers 1 to 9) Some people ignored single digits.
try it yourslef
c=0
for num in range(10,2*10**6):
if str(num) == str(num)[::-1]:
c+=num
print(c)
[\code]
>20 == 020?!?
Any confirmation of this or should we ignore leading zeroes?
Bumping for an answer to this.
Sup Forums still programs, right?
sum(x for x in range(2000001) if str(x) == str(x)[::-1])
python is the only language that can make me cum
(1..2000000).select{|i| i.to_s == i.to_s.reverse}.reduce(&:+)
(defun is-palindrome (num)
(equal (write-to-string num) (reverse (write-to-string num))))
(let ((sum 0))
(loop for x from 1 to 2000000 do (when (is-palindrome x) (incf sum x)))
(format t "Sum of all primes below 2000000 = ~a~%" sum))
>using loop with lisp
Retarded frogposter.
How would you do it?
fuck off retard
It is a legitimate question. Your inability to answer it is sickening.
Should this be
{b, 1, 9} instead of {b, 1}
no idea what language that is
the fact you are on this board is sickening
literally learn to google you stupid mongoloid
The fact that you exist is sickening.
nope but the world's longest palindromic word is saippuakivikauppias and it means soapstone seller
with reduce and filter ofc
#include
#include
#include
#define LIMIT 2000000
int int_pow(int base, int exp) {
int result = 1;
while (exp) {
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
// 123 will return 123321
long long make_palindrome(const int num) {
long long result;
char l = log10(num);
// e.g if num is 123, result becomes 123000
result = num * pow(10, l+1);
char i;
for (i = 1; i
way off
see and and
Fixed the main function, now it gives the correct answer 2045041040
I skipped all the numbers which begin with 200-999 eg 200002
int main(void) {
struct timeval start, end;
long long sum = 45; // Sum of numbers 1-9
// Largest palindrome under 2000000 is 1999991 which is 199 with 9
// in the middle so the loop only has to go up to 199.
int i, j;
long long temp;
for (i = 1; i
No b must be less than 2 as it's the "millions". It's Mathematica. It's the sum for the 1-2 million palindromes.
IS IT A COINCIDENCE YOU POSTED A RACECAR BECAUSE RACECAR IS A PALINDROME
#include
typedef struct { char length; char digits[7]; } digits;
digits getDecimalDigits(unsigned long value) {
digits extractedDigits = {0, {0, 0, 0, 0, 0, 0, 0}};
while (value) {
extractedDigits.digits[extractedDigits.length] = value % 10;
extractedDigits.length++;
value = value / 10;
}
return extractedDigits;
}
char digitsArePalindrome(digits theDigits) {
char left = 0;
char right = theDigits.length - 1;
if (theDigits.length) {
while (right > left) {
if (theDigits.digits[left] != theDigits.digits[right]) {
return 0; }
left += 1; right -= 1;
}
return 1;
}
return 0;
}
int main(int argc, const char * argv[]) {
digits tempDigits;
unsigned long total = 0;
for (unsigned long i = 1; i < 2000000; i++) {
tempDigits = getDecimalDigits(i);
if (digitsArePalindrome(tempDigits)) { //printf("palindrome: %lu\n", i);
total += i; }
}
printf("Sum of all palindromes numbers under 2 million: %lu\n", total);
return 0;
}
Sum of all palindromes numbers under 2 million: 2045041040
mine is faster lulz
>Sum[b 10 ^6 + a 10^5 + c 10^4 + d 10^3 + c 10^2 + a 10 + b, {c, 0,
9}, {a, 0, 9}, {d, 0, 9}, {b, 1}]
Can someone please break down this equation / explain it in english?
Yeah just wait a few minutes.
>Optimizing non-critical sections.
I respect the attitude but is a huge time-waster for little benefit.
> sum [ x | x
It's generating the palindromes of the form
bacdcab
and adding them up, where 0
>The only hard part that requires any work is between 1x10^6-2x10^6
Sum of all palindrome numbers between 100 and 200: 1460
Sum of all palindrome numbers between 1000 and 2000: 14960
Sum of all palindrome numbers between 10000 and 20000: 1499600
Sum of all palindrome numbers between 100000 and 200000: 14999600
Sum of all palindrome numbers between 1000000 and 2000000: 1499996000
What is the formula?
OP here, I didn't even realize this when I made the thread
Bumping in hopes that someone can figure out the formula for the sum of all palindrome numbers between 10^x and 2 * 10^x.
Base cocaine
literally not how arabic numerals work
in ruby:
(1..2_000_000).select{|e| e.to_s == e.to_s.reverse}.sum
>2_000_000
Disgusting.
Why? for constant numbers it makes it easier to read
The human eye can parse... 9 zeros
ARE YOU NOT HUMAN!?!!? WHAT ARE YOU?????
Java 8 Bitches!
import java.util.stream.LongStream;
public class App {
public static void main(String[] args) {
System.out.println(new App().sumUpTill(2000000L));
}
long sumUpTill(Long arg) {
return LongStream.range(1, arg)
.filter(this::isP)
.sum();
}
boolean isP(Long l){
String str = l.toString();
return str.equals(new StringBuilder(str).reverse().toString());
}
}
yep because fuck readability amirite? if it saves one person on your team from misreading the constant (and subsequently fucking up somewhere else) it's totally not worth it
>new App()
dude what
method references in streams don't work in a static context.
It could be rewritten without the static reference like this:
import java.util.stream.LongStream;
public class App {
public static void main(String[] args) {
System.out.println(sumUpTill(2000000L));
}
static long sumUpTill(Long arg) {
return LongStream.range(1, arg)
.filter(l -> isP(l))
.sum();
}
static boolean isP(Long l){
String str = l.toString();
return str.equals(new StringBuilder(str).reverse().toString());
}
}
>Having people on your team whose eyes are incapable of accurately parsing 9 zeroes...
No wonder most software these days has huge problems.
NINE ZEROS
most software these days is developed in spaces where hiring managers are rubbing the backs of 30 year old wannabes playing xbox at work and talking to the news about work life balance when asked to work overtime to complete a project on time
This isn't even worth arguing with. The end result is the same, but using the 2_000 notation is more readable, there is no denying that.
So why not use it other than to feel better than everyone like you are so demonstrating here?
Or just do this
import java.util.stream.LongStream;
public class App {
public static void main(String[] args) {
System.out.println(sumUpTill(2000000L));
}
private static long sumUpTill(Long arg) {
return LongStream.range(1, arg)
.filter(App::isP)
.sum();
}
private static boolean isP(Long l){
String str = l.toString();
return str.equals(new StringBuilder(str).reverse().toString());
}
}
This looks ugly as fuck, functional programming does not belong in Java.
>sum [ x | x
Good going, pythonista.
Your post is invalid.
>Arguing in favor of having incompetent members in your team
Your argument is invalid.
That was Haskell code you dumbass.
Disgustingly slow
>Disgust at people not optimizing non-critical sections
Disgustingly sub-optimal in the time-dimension
I disagree, its so nice to have FP in Java. It makes collection processing so much quicker thanks to parallelStream(). Automatic true Fork/Join multithreading for free!
Also the fact that java.util.Logging uses supplier concepts now it saves a lot of string concatenation unless its actually supposed to log. Good stuff all around.
I'm not saying they're incompetent, the fact of the matter is that people make mistakes. Using the 2_000 notation lowers the chance of making a mistake, it doesn't mean that they're fucking incompetent. Christ this is getting ridiculous.
Going by your logic we should all code without comments because who needs comments unless you're incompetent!
Sup Forums in a nutshell
how do you know the disgust was aimed at the programmer and not the high-level and inefficient abstractions they used?
Run that code and come back to me tomorrow when it finishes
>Using Haskell
>Calling other people dumbass
ugh...
I didn't write it, ya big dum dum.
It seems useful to have, but what the hell is with the ::? I thought we left that mess behind with C++.
2000000000
2_000_000_000
and most people would still have to look twice for billion or million
people are fucking idiots, get over it. just because we're able to overcome our stupidity to smash atoms means nothing yet
Oh yeah I agree with you there, :: is a terrible notation for a method reference. C++ begone!
1.3 seconds runtime.
Given the question we are trying to answer it would be a sickening waste of our time to try to construct any kind of elaborate, generic, or abstract solution. (simple solution: few minutes to conceive, 1.3 seconds runtime; complex solution: 15+ minutes to conceive and type, 0.5 seconds runtime)
You have yet to invalidate the argument that it is disgustingly inefficient to have incompetent people on your team whose eyes can't even parse 9-12 zeroes.
CS major answer
>Make a 2 million loop
>make a reverse function
>check if current number equals reversed
>add number to a sum
Not the guy you're arguing with, but not everyone is a 10x-er like everyone here on Sup Forums (loljk). So I think his point is valid.
In the corporate world (or any job with a team bigger than like 5 people really) readability is more important than everything else, and the underscore notation does improve that.
no no no, i cant. people who can't figure our figures are a cancer
Time to run the code in :
0.255411s
Python confirmed 6.5 times slower than C
lets throw the java version in here too!
...
Nice. Post specs.
:: is valid Java syntax when?
My initial thought was this. It makes me feel sad and brainlet
When the JVM gets nice and warmed up and JITs it its even faster!
Xeon E3-1231v3 + 16gb ddr3! haha
Java 8, method references and Functional Programming =
hmm some of that new functional stuff sounds pretty intense
Why?