-Alright im supposed to make a program that tests for perfect numbers. I have to use 2 methods and 3 methods if you count the main method.
-My first method has to return a boolean value of true or
false if the argument is or isn't a perfect number. I found that all perfect numbers either end in a 6 or an 8 so i was thinking i could just use something like... - if (value1 % 2 = 0)
-
{
-
value1 = Perfect number
-
}
-
else
-
{
-
value1 = Not perfect
-
}
-Im confused on that if/else statement though because im not supposed to print out anything until my next method. And i know that if/else statement wont work so anyone know how to fix it?
-My last method has to take the users integer argument and print out the factors of the argument in descending order (largest to smallest). This is the part of the code im most confused on. I have no clue how to do this
- When my program is done its supposed to look like...
Please enter a possible perfect number: 6
6:3 2 1
or
Please enter a possible perfect number: -2
-2:NOT PERFECT
If anyone has any ideas or can help me i would really appreciate the help.
17 9293 JosAH 11,448
Recognized Expert MVP
What is the definition of a perfect number? Start from there and forget the
divisability by six or eight. That is just a necessary but not a sufficient
condition.
kind regards,
Jos
well a a perfect number is defined as a positive integer which is the sum of its proper positive divisors. Like
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
but they found all perfect numbers end in 8 and 6 so thats why i came up with that idea
JosAH 11,448
Recognized Expert MVP
well a a perfect number is defined as a positive integer which is the sum of its proper positive divisors. Like
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
but they found all perfect numbers end in 8 and 6 so thats why i came up with that idea
That's what I wrote: it is just a necessary but not a sufficient condition, i.e. is
8 a perfect number, or 16 or 18 or 26? You have to find the divisors of a number
and add them to be able to see whether or not a number is a perfect number.
Do you know how to find the divisors of a number?
kind regards,
Jos
That's what I wrote: it is just a necessary but not a sufficient condition, i.e. is
8 a perfect number, or 16 or 18 or 26? You have to find the divisors of a number
and add them to be able to see whether or not a number is a perfect number.
Do you know how to find the divisors of a number?
kind regards,
Jos
no i have no idea how im going to find the divisors of a number. Im thinking some kind of loop though?
JosAH 11,448
Recognized Expert MVP
no i have no idea how im going to find the divisors of a number. Im thinking some kind of loop though?
Yep, and also have a look at the % (modulo) operator: a % b is the remainder of
a when you divide it by b; e.g. 7 % 3 == 1, 10 % 4 == 2, 12 % 3 == 0 etc.
Here's a simple loop: -
int a= 12;
-
for (int b= 1; b <= a; b++)
-
if (a%b == 0) System.out.println(b);
-
Try it for a couple of values for 'a' and see how you can use that loop.
kind regards,
Jos
well idk how im gonna do it because how u have the variable a=12, the loop has to check the users input value which could be like 232434 lol
There is a fairly comprehensive old thread talking about coding perfect numbers in java at http://bytes.com/forum/thread17338.html
To learn more about perfect numbers in general check out http://mathworld.wolfram.com/PerfectNumber.html
To answer your last post you can get user input with - String text = in.readLine();
-
int x = Integer.parseInt(text);
or use java.util.Scanner - Scanner keyboard = new Scanner(System.in);
-
int number = keyboard.nextInt();
You'll definitely need to write a loop that finds divisors. JosAH's loop works. However int b just needs to incremented to a/2 since a divisor won't be larger than half the number except of course the number itself (number * 1 = number). Or better yet the loop should only go up to floor(sqrt(number)) since the larger divisor can be calculated with the smaller divisor.
When a divisor is found you'll want to add it to a cumulative sum to see if the sum of the divisors are equal to the inputted number. Or since you need to output the divisors later anyway just store them in an array of ints then add them and see if the sum is equal to the inputted number.
So according to the Wolfram site the first eight perfect numbers are 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, and 2305843008139952128. The last three are out of the range of the int data type (2^31 or 2147483648). Also they would take a very long time to calculate anyway.
Hope this helps. There is plenty of code around the internet about perfect numbers. Look at some samples to help you along.
so something like
for (int b = 1; b < value; b++)
{
if ( value % b == 0)
{
somehow store the number
}
idk how i would store the number lol help?
alright i found a loop that helps solve it, but i need to some how keep track of the divisors and output them. this loop would work but it doesnt really keep track of the divisors. this is my whole code so far - import java.util.Scanner;
-
-
public class test
-
{
-
public static void main ( String args[] )
-
{
-
Scanner in = new Scanner ( System.in );
-
-
int value, value1;
-
-
-
do
-
{
-
System.out.print ( "How many numbers would you like to test? " );
-
value = in.nextInt();
-
}
-
while (value < 1);
-
-
for (int a = 0; a < value; a++)
-
{
-
System.out.printf ( "Please enter a possible perfect number ");
-
value1 = in.nextInt();
-
}
-
-
-
public static testPerfect()
-
{
-
-
sumSoFar = 0;
-
-
for ( a = 1; a < value1; a = a + 1 )
-
{
-
if ( value1 % a == 0)
-
sumSoFar = sumSoFar + i;
-
}
-
-
-
-
}
-
}
alright right now im at - import java.util.Scanner;
-
-
public class test
-
{
-
public static void main ( String args[] )
-
{
-
Scanner in = new Scanner ( System.in );
-
-
int value, value1, sumSoFar = 0, sumofDivisors;
-
-
-
do
-
{
-
System.out.print ( "How many numbers would you like to test? " );
-
value = in.nextInt();
-
}
-
while (value < 1);
-
-
for (int a = 0; a < value; a++)
-
{
-
System.out.printf ( "Please enter a possible perfect number ");
-
value1 = in.nextInt();
-
}
-
-
-
for ( b = 1; b < value1; b = b + 1 ) {
-
if ( value1 % b == 0)
-
sumSoFar = sumSoFar + b;
-
}
-
sumOfDivisors = sumSoFar;
-
-
if ( value1 == sumOfDivisors )
-
System.out.println( "The value " + numberToCheck + " is PERFECT" );
-
else
-
System.out.println( "The value " + numberToCheck + " is NOT PERFECT" );
-
-
-
}
-
}
but i get these compile errors
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:27: cannot find symbol
symbol : variable b
location: class test
if ( value1 % b == 0)
^
test.java:28: cannot find symbol
symbol : variable b
location: class test
sumSoFar = sumSoFar + b;
^
test.java:28: operator + cannot be applied to int,b
sumSoFar = sumSoFar + b;
^
test.java:28: incompatible types
found : <nulltype>
required: int
sumSoFar = sumSoFar + b;
^
test.java:30: cannot find symbol
symbol : variable sumOfDivisors
location: class test
sumOfDivisors = sumSoFar;
^
test.java:32: cannot find symbol
symbol : variable sumOfDivisors
location: class test
if ( value1 == sumOfDivisors )
^
10 errors
anyone know how to fix these
JosAH 11,448
Recognized Expert MVP
Compare your lines #19 (correct) and #26 (where the problem occurs): -
for (int a = 0; a < value; a++) // <--- line #19
-
{
-
System.out.printf ( "Please enter a possible perfect number ");
-
value1 = in.nextInt();
-
}
-
-
-
for ( b = 1; b < value1; b = b + 1 ) { // <--- line #26
-
Do you notice anything?
kind regards,
Jos
jkmyoung 2,057
Recognized Expert Top Contributor
If this is a confusing post, please ignore it. I'm not sure of your mathematics level.
As an alternative (maybe faster?) way of solve your problem, instead of finding all divisors of your number, have you considered prime factoring?
Eg, let your number be n = a^e1 * b^e2 * c^e3 *...
where a, b, c, ... are prime numbers and
e1, e2, e3, ... are the corresponding exponents.
Therefore all factors of the number n are in the form
a^f1 * b^f2 * c^f3* ...
where 0 <= f1 <= e1, 0<=f2<=e2, ....etc
If you sum up all factors
Sum(f1 = 0 to e1) of [Sum (f2 = 0 to e2) of .... [a^f1 * b^f2 * c^f3* ...]...]]
Using the sum of geometric series you get
= [Sum (f2 = 0 to e2) of .... (a^(e1+1) - 1)/ (a-1) *[b^f2 * c^f3* ...]...]]
= (a^(e1+1) - 1)/(a+1) * (b^(e2+1) - 1)/(b+1) * ....
Which for a perfect number = 2 * n
So if you found the prime factorization, you could sum up the numbers much faster by using the proper multiplication.
============
If you really wanted to cheat, you could check if the number is in the form 2^n * (2^(n+1) - 1) where (2^(n+1) - 1) is prime. eg, for:
n=1: (2)(3) = 6
n=2: (4)(7) = 28
n=3: (8)(15) = 120 (15 is not prime)
n=4 (16)(31) = 496 ...
etc.
JosAH 11,448
Recognized Expert MVP
127 is prime but 64*127 == 8128 is not a perfect number. That check is just
a necessary but not a sufficient condition. Finding the factors of a number n
is just a O(n) operation so a predicate for a perfect number just takes O(n)
(neglecting the costs for a division etc)
kind regards,
Jos
ok i have my code here -
import java.util.Scanner;
-
-
public class test
-
{
-
public static void main(String[]args)
-
{
-
-
Scanner in = new Scanner ( System.in );
-
-
int sum=0, x=0, value1 = 0, value;
-
-
do
-
{
-
System.out.print ( "How many numbers would you like to test? " );
-
value = in.nextInt();
-
}
-
while (value < 1);
-
-
for (int a = 0; a < value; a++)
-
{
-
System.out.printf ( "Please enter a possible perfect number ");
-
value1 = in.nextInt();
-
-
public static testPerfect()
-
{
-
for(int factor=1; factor < value1; factor++){
-
-
x = value1 % factor;
-
-
if(x==0)
-
-
sum=sum+factor;}
-
}
-
-
public static printFactors()
-
{
-
-
if(sum!=value1)
-
{
-
System.out.printf("%d:NOT PERFECT", value1);
-
}
-
-
if(sum==value1)
-
{
-
System.out.printf ( "%d: ", value1);
-
-
-
for(int factor=1;factor<value1;factor++)
-
{
-
x=value1%factor;
-
if(x==0)
-
System.out.printf("%d ", factor);
-
}
-
}
-
}
-
System.out.print("\n");
-
sum=0;
-
}
-
}
-
}
however i need 2 public static method
first one is testPerfect() and it goes around this part of my code - for(int factor=1; factor < value1; factor++){
-
-
x = value1 % factor;
-
-
if(x==0)
-
-
sum=sum+factor;}
the other method is to be called printFactors() and its to go around -
if(sum!=value1)
-
{
-
System.out.printf("%d:NOT PERFECT", value1);
-
}
-
-
if(sum==value1)
-
{
-
System.out.printf ( "%d: ", value1);
-
-
-
for(int factor=1;factor<value1;factor++)
-
{
-
x=value1%factor;
-
if(x==0)
-
System.out.printf("%d ", factor);
-
-
}
-
}
-
System.out.print("\n");
-
sum=0;
im not exactly sure how to do this every time i try i get compile errors, anyone know how to do this?
JosAH 11,448
Recognized Expert MVP
At line #24 you're trying to define a (static) method inside the body of a for loop
(started at line #19). It is not allowed to define a method inside the body of
another method.
kind regards,
Jos
ps. Please do read that tutorial because you're not up to the basics yet.
jkmyoung 2,057
Recognized Expert Top Contributor
127 is prime but 64*127 == 8128 is not a perfect number.
? I'm pretty sure 8128 is a perfect number.
JosAH 11,448
Recognized Expert MVP
? I'm pretty sure 8128 is a perfect number.
You're right; I manually miscalculated it
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: |
last post by:
I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement. What I need
to know is, is there any method in Java to...
|
by: gk245 |
last post by:
Trying to write a program that will figure out if a number is perfect
or not.
Here is my logic:
1) Read in the number
2) Split it up (number - 1)
3) Put all the split up numbers into an...
|
by: psalmprax |
last post by:
who can write a C-program on perfect numbers from 1 to 1000?
|
by: The 1 |
last post by:
Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
* PEfect power is a no...
|
by: Sheepman |
last post by:
Could someone please walk me through the following program. It's only 15 lines. My comments and questions are below dealing mostly with the array and "for" loops. Hopefully my questions make sense as...
| |
by: jayoh12 |
last post by:
create a program that will let the user to enter a number..determine if the number is perfect or not...
e.g
6 is perfect # bec. its factors are 1,2,3 and 6.
1+2+3+6-6=6
6=6
|
by: wkid87 |
last post by:
Using C# Microsft Visual C#, Console Application
I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction:
...
|
by: hipfunkyjive |
last post by:
Basically my program has to find perfect numbers in a range 1- 1000
it works , kinda but it displays a few numbers that are not perfect for example the first Perfect numbers are : 6 28 496
my...
|
by: Busgosu |
last post by:
i dont know how to upload in a way that you can see it clearly.. so im just going to copy and paste what i have so far... sorry. :S
#include <iostream>
using namespace std;
#include <cmath>...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |