Reducing Number of Multiplications in a Loop

  • Context: Undergrad 
  • Thread starter Thread starter rwinston
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around optimizing the calculation of palindromic numbers generated as the product of two three-digit numbers. Participants explore methods to reduce redundant multiplications and address the issue of duplicate palindromic results.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a naive approach using nested loops to calculate products of two three-digit numbers, which leads to redundant multiplications.
  • Another participant suggests modifying the loop to avoid redundant calculations by iterating with the condition j >= i, but notes that duplicates still occur.
  • A participant proposes that sorting results and removing duplicates might be a simpler solution, while expressing interest in the count of unaccounted palindromes and their primality.
  • Another participant offers two efficient methods: looping through three-digit numbers with a condition on the product, or looping through existing palindromes and factoring them, questioning which method is faster.
  • There is a discussion about the implications of skipping products that are multiples of 10 and whether leading zeros should be considered in palindromic numbers.

Areas of Agreement / Disagreement

Participants express uncertainty about the existence of a straightforward solution to avoid duplicate palindromic products. Multiple competing methods are proposed, but no consensus is reached on the best approach.

Contextual Notes

Participants acknowledge the complexity of the problem and the potential variations in outcomes based on the chosen method of calculation. There are also considerations regarding the definitions of palindromic numbers and the treatment of leading zeros.

rwinston
Messages
36
Reaction score
0
Hi

Following on from my palindromic number question, if I am calculating a set of palindromic numbers generated as the product of two three-digit numbers, I could generate the entire product set and then test each number for the palindrome property.

the naive approach would be a loop like

for (i in 999:100)
for (j in 999:100)
p = i * j

However, we do many redundant multiplications this way, so the obvious approach is to do

for (i in 999:100)
for (j in i:100)
p= i * j

However, I still a lot of palindromic numbers that are duplicated. for instance, the palindrome 444444 is generated by:

962 * 462 = 444444
924 * 481 = 444444
858 * 518 = 444444
814 * 546 = 444444
777 * 572 = 444444

Is there any way to avoid or predict these multiplications of two 3-digit numbers that will produce duplicate palindromes?
 
Last edited:
Physics news on Phys.org
rwinston said:
Hi

Following on from my palindromic number question, if I am calculating a set of palindromic numbers generated as the product of two three-digit numbers, I could generate the entire product set and then test each number for the palindrome property.

the naive approach would be a loop like

for (i in 999:100)
for (j in 999:100)
p = i * j

However, we do many redundant multiplications this way, so the obvious approach is to do

for (i in 999:100)
for (j in i:100)
p= i * j

However, I still a lot of palindromic numbers that are duplicated. for instance, the palindrome 444444 is generated by:

962 * 462 = 444444
924 * 481 = 444444
858 * 518 = 444444
814 * 546 = 444444
777 * 572 = 444444

Is there any way to avoid or predict these multiplications of two 3-digit numbers that will produce duplicate palindromes?

You asked a very tough question which I doubt has an answer. It would be much simpler to just automatically sort your results and remove duplicates. I would be interested in determining how many of the palindromes between the smallest and largest in your list are unaccounted for by your program and how many of these are prime. Each answer is going to be different.
 
Last edited:
Thanks Ramsey. I didnt think there would be a straightforward answer to this one.
 
rwinston said:
Thanks Ramsey. I didnt think there would be a straightforward answer to this one.

There are two reasonably efficient ways of doing this that immediately spring to mind. You could loop through the three-digit numbers, letting i <= j (405,050 loops), possibly skipping ij = 0 mod 10 (reducing the possibilities slightly at the cost of overhead). Alternately, you could loop through the 5 and 6-digit palindromes (1800 loops), factoring at each step. You have to decide which is faster -- in essence, is factoring harder or easier than 225 multiplications?
 
CRGreathouse said:
There are two reasonably efficient ways of doing this that immediately spring to mind. You could loop through the three-digit numbers, letting i <= j (405,050 loops), possibly skipping ij = 0 mod 10 (reducing the possibilities slightly at the cost of overhead). Alternately, you could loop through the 5 and 6-digit palindromes (1800 loops), factoring at each step. You have to decide which is faster -- in essence, is factoring harder or easier than 225 multiplications?
If you skip ij= 0 mod 10 then you would miss 012210 = 110*111 or are leading zeros prohibited?
 
ramsey2879 said:
If you skip ij= 0 mod 10 then you would miss 012210 = 110*111 or are leading zeros prohibited?

Typically multiples of b aren't considered base-b palindromes. For example, Sloane's http://www.research.att.com/~njas/sequences/A002113 doesn't include 10 = 010 or 100 = 00100. But if you wanted to consider them, you'd of course need to include the multiples of 10.
 
Last edited by a moderator:

Similar threads

  • · Replies 8 ·
Replies
8
Views
8K
  • · Replies 26 ·
Replies
26
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 50 ·
2
Replies
50
Views
12K
  • · Replies 3 ·
Replies
3
Views
827
  • · Replies 1 ·
Replies
1
Views
2K