Translate Pseudocode and get the result

210 Views Asked by At

Can you help translating this Pseudocode into java and get the result as I tried but didn't get the results and I'm starting to learn Java.

num=2
create list called list
while(true){
empty list
bool = false
for i=1 to num-1
if(num mod i==0){
add i to list
if((i &(bitwise)1)==1)
bool=!bool
}
if sum(list)==num && bool//logical and
return num; //Found the number we seek
num++
}

This is Java translating but I'm not sure as it didn't get the result numbers

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.ArrayList;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        //set a number
int num=2;
//make list called list
ArrayList<Integer> list = new ArrayList<Integer>();
//run this while true, but just saying true makes an infinite loop 
boolean bool = true;
while(bool){
//clear the list
list.clear();
//set
bool = false;

for (int i= 1; i < num-1; i = i + 1){
    //If number modulo i is 0
    if((num % i) ==0){
        list.add(i);
        //If bitwise and of i and 1 is true
        //This is doing things with the underlying binary
    if((i & 1)==1)
        //Reverse the valuse of bool 
        bool = !bool;
    }
int sum = 0; 
//for each element in the list
for(int x: list){
    //add it to sum
    sum += sum;
}
//execute is sum and num are the same and bool is true
if ((sum == num )&& bool){}
    return num; 
    //This does nothing as it is never reached. return ends your execution 
    num++;
}
} 
    }
}
2

There are 2 best solutions below

0
On

It will never get the result, most probably at least.

As someone already said it is trying to find "the smallest Perfect Number that has odd number of odd factors", and any number that have in its sum odd number of odd numbers is odd. So its searching for odd perfect number which is yet to proven if it exist or not that is why it will never give you result fast enough. It could take million years it could take forever. The question was supposed to be solved by logic.

BTW: Actually I am the one who made this pseudocode, better luck solving the code next weak. (:

0
On

This sure looks like some assignment. Anyways, here's the translation of the pseudocode:

public int code ()
{
    int num = 2;
    List<Integer> list = new ArrayList<> ();

    while (true)
    {
        list.clear ();
        boolean bool = false;

        for (int i = 1; i < num; ++i)
        {
            if (num % i == 0)
            {
                list.add (i);

                if ((i & 1) == 1)
                {
                    bool = !bool;
                }
            }
        }

        if (sum (list) == num && bool)
        {
            return num;
        }

        num++;
    }
}

private int sum (List<Integer> list)
{
    return list.stream ().mapToInt (i -> i).sum ();
}

The code is sure taking time to print out any result.

Try calling the code method and you'll see.

Apart from that, what number this code is trying to find, you may ask?

Well, through logical deduction, I can tell that this code is trying to find the smallest Perfect Number that has odd number of odd factors. Try thinking about it.