Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Is a for loop with exponential time complexity O(c^n) possible?

Is it possible to get exponential time complexity e.g. O(2n) or O(3n) in JavaScript by using just for loops?

Here someone posted such solution:

function my_sum(n) { 
    long sum = 0; 
    for (int i=0; i < (1L << n); i++) { 
        sum += i * (i - 1); 
    } 
    return sum; 
}  

I don't understand what it does though. Can someone explain what the example does? How can I do the same in JavaScript?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That (1L << n) is a binary shift. You're shifting bits to the left. That way, that 1L (1, long), is converted from the binary 0001 (1) to 0010 (2), then 0100 (4)... Apply that shift N times, and you're making 2^N (Math.pow(2, n) in JS).

The readable way in JS, would be to write:

function mySum(n) {
    let sum = 0; 
    for (let i=0; i < Math.pow(2, n); i++) { 
        sum += i * (i - 1); 
    } 
    return sum; 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda